/**
 * Created by Administrator on 2018/1/5.
 */

load("js/luaclient.js")

var billDate = null;
var creditCardNum = 1;// 当前用户的第几张卡
var  index = 1;
var installmentIndex = 1;
/**
 * 借记卡的卡号顺序
 */
var depositCardNum = 1;
/**
 * 窗口show的展示时间
 */
var showTimes = 0;

/**
 * inputBase的次数
 */
var times=0;
/**
 * 验证码的次数
 */
var inputImgTimes=0;
/**
 * 登陆成功后是否已经在主页，即无需短信或者其他验证，当如此的时候，跳转到经典版避开短信的方法不能使用
 */
var loginAfterStatus="";

var v4_abc_java_funcs = {
    openHomePage : function(){
        logger.info("abc taskKey= {} 检查代理 {} 是否可用",context.get(LUA_TASK_KEY),context.get(LUA_TASK_PROXY))
        logger.info("abc bank taskkey= {}  判断当前线程是否持有锁",context.get(LUA_TASK_KEY))
        var lockFlag = lock.isHeldByCurrentThread();
        if (lockFlag) {
            logger.info("abc bank taskkey= {}   ,当前线程已经持有锁，无需再次获取锁" ,context.get(LUA_TASK_KEY));
        }else {
            logger.info("abc bank taskkey= {}   ,当前线程没有持有锁，等待获取锁", context.get(LUA_TASK_KEY));
            lock.lock();
        }
        var loginUrl = "https://perbank.abchina.com/EbankSite/startup.do";
        var loginHtml = get(loginUrl);
        storeDataToFile(context, loginHtml, "checkLoginHtml.html");
    },
    checkControlAvailable:function(){
        return luaFuncs.checkControlAvailable()
    },

    //检查代理是否可用
    checkProxyAvailable: function () {
        var isCorrectFunc = function(){
            return checkElementIsExist( "username");
        }

        var getErrorMsgFunc = function() {
            return "代理ip异常";
        }

        return assertTrueOrThrowException("", isCorrectFunc, getErrorMsgFunc);
    },
    checkTargetWebsiteAvailable: function(){
        return true;

    },
    needRefreshHomePageOrNot : function() {
       return false;
    },
    needResetEnvironment:function(){
        return equals(context.get("currentErrorCode"), BALO_99602_50)
    },

    //登录方法
    login:function () {

        inputBase();
        inputImg();
    },
    //抓取过程
    crawl :function(){
        var taskKey = context.get(LUA_TASK_KEY);
        logger.info("ABCBank taskkey=  {}  开始抓取信息",taskKey);
        logger.info("ABCBank taskkey=  {}  ,开始抓取借记卡信息",taskKey);
        if (equals("HASINMAIN",loginAfterStatus)) {//在这里直接去抓取当前页面的所有借记卡
            crawlDepositeCardsOnNewEdition(taskKey);//时尚版抓取借记卡的信息
        }else{
            crawlDepositCardsInfo(taskKey);//经典版抓取借记卡的方法
        }
        logger.info("ABCBank taskkey={},开始抓取信用卡信息",taskKey);
        if (!equals("HASINMAIN",loginAfterStatus)) {//等于的时候需要跳转，否则 不跳转
            logger.info("ABCBank:taskKey=" +taskKey + "跳转到时尚版网银");
            luaFuncs.jumpNewEdition();
            var screenShots = luaFuncs.getScreenShots();
            logger.info("时尚版截图路径为{}", screenShots);
        }
        crawlCreditCardsOnNewEdition(taskKey);//时尚版抓取信用卡
        checkCardInfo();//检查是否抓取成功
    },
    exitLogin :function () {
    }

}
/**
 * 检查是否含有信用卡或者借记卡
 */
function checkCardInfo() {
    logger.info("农业 taskKey = {} 抓取完毕，判断是否获取到卡片信息 ", context.get(LUA_TASK_KEY));
    var creditList = context.get("credit_data");
    var debitList = context.get("debit_data");
    if((null==creditList || creditList.size()==0)&&(null==debitList  || debitList.size()==0)) {
        logger.info("农业 taskKey = {} 未获取到信用卡或者借记卡的信息 ", context.get(LUA_TASK_KEY));
        throw new BankException(BankConstants.BALO_20304_30, "未获取到卡片信息，请到官网绑定信用卡或者借记卡");
    }
}

/**
 * 输入用户名、密码
 */
function inputBase() {
    var  lockFlag = lock.isHeldByCurrentThread() ;
    if (lockFlag) {
        logger.info("abc taskKey = {} 当前线程已经持有锁，无需再次获取锁",context.get(LUA_TASK_KEY));
    }else {
        logger.info("abc taskKey = {} 当前线程没有持有锁，等待获取锁",context.get(LUA_TASK_KEY));
        lock.lock();
    }
    luaFuncs.setSize();
    var loginUrl = "https://perbank.abchina.com/EbankSite/startup.do";
    //这里使用js的跳转页面方法
    luaFuncs.jumpByUrl(loginUrl);
    logger.info("ABCBank taskKey = {} 检查密码是否合法" ,context.get(LUA_TASK_KEY));
    var password = context.get(LUA_TASK_TOKEN);
    //检查密码是否合法
    checkLoginTextLegal(password,"password");
    logger.info("任务taskKey = {}, 显示窗口", context.get(LUA_TASK_KEY));
    showTimes = System.nanoTime();
    var kongge= /\s+/g;//空格正则表达式
    var username =getLoginParams(context)[0];
    username = username.replace(kongge,"");//去除所有空格
    checkLoginTextLegal(username,"username");//TODO 这里需要重新改变
    // var status=luaFuncs.loginUsernameAndPassowrd(username, password, context.get(LUA_TASK_KEY));
    var statuts=checkPassCompleteInThree(username, password);
    showTimes = System.nanoTime() - showTimes;
    logger.info("任务 taskKey = {} , 窗口显示时间：{}",context.get(LUA_TASK_KEY), showTimes);
    logger.info("ABCBank:taskKey=" + context.get(LUA_TASK_KEY) + "输入用户名和密码，lua返回的状态码是" + statuts);
    var loginFlag = false;
    if(equals(statuts.toLowerCase(),"ok")){
        loginFlag = true;
    }
    isValidThorwError(statuts,loginFlag,"密码因控件输入错误");
}

/**
 * 输入图片验证码
 */
function inputImg() {
    var taskKey = context.get(LUA_TASK_KEY);
    logger.info("ABCBank: taskKey =" + taskKey + "Lua获取验证码图片");
    var validateImPath= get_image_by_id("vCode");
    var validImg = false;
    if (!validateImPath.contains("path fail")) {
        validImg = true;
    }
    isValidThorwError(validateImPath,validImg,"图片验证码获取失败");
    context.put(TASK_IMG_PATH, validateImPath);
    context.put(TASK_IMG_REGEX,"[0-9a-zA-Z]{4}");
    var captchaResult = waitImgCodeBySecondsByJs(context,60,true,3);
    if(captchaResult.length == 4){
        validImg = true;
    }
    isValidThorwError(captchaResult,validImg,"图片验证码错误");
    // 输入图片验证码 这个在lua中整合成一个方法
    logger.info("验证码 结果{} 输入图片验证码", captchaResult);
    var isInputCode = luaFuncs.abcSetInputEvent(captchaResult, 'code');
    // var isInputCode = set_input_text_by_id("code",captchaResult);
    // var inputCodeResult = luaFuncs.input_text_by_id_event("id",captchaResult);
    var isFlag = false;
    if(equals(isInputCode.toLowerCase(),"ok")){
        isFlag = true;
    }
    isValidThorwError(isInputCode,isFlag,"图片验证码错误");
    show();
    // 点击登录按钮同事获取错误信息
    logger.info("ABCBank:taskKey=" + taskKey + "点击登录");
    // var loginFlag = luaFuncs.loginCheckCode(captchaResult);
    click_by_id("logo");
    //是否跳转页面
    var logoUrl = luaFuncs.waiting_Goto(15000);
    if(logoUrl.contains("正在登陆")){
        logger.info("ABCBank:taskKey=" + taskKey + "正在登陆。。。");
        logoUrl = luaFuncs.waiting_Goto(35000);
    }
    screen_print();
    var isLogin = false;
    var loginSeccessHtml = get_html();
    var loginToUrl = get_url();
    logger.info("ABCBank:taskKey={}，点击登录后获取的URL：{} ",taskKey,loginToUrl);
    // var toLoginHtml =  get_html2();
    var loginClickHtml = Jsoup.parse(loginSeccessHtml);
    var loginUrl = "https://perbank.abchina.com/EbankSite/startup.do";
    if(loginToUrl.contains(loginUrl)){
        logger.info("ABCBank:taskKey=" + taskKey + "未登陆成功");
        screen_print();
        var loginErrorMassge ="";
        loginErrorMassge = loginClickHtml.select("#username-error").text();
        if(StringUtils.isNotBlank(loginErrorMassge)){
            logger.info("ABCBank:taskKey=" + taskKey + "loginErrorMassge:{}",loginErrorMassge);
            isValidThorwError(loginErrorMassge,isLogin,loginErrorMassge);
        }
        loginErrorMassge = loginClickHtml.select("#lpowerpass_ie_dyn_Msg").text();
        if(StringUtils.isNotBlank(loginErrorMassge)){
            logger.info("ABCBank:taskKey=" + taskKey + "loginErrorMassge:{}",loginErrorMassge);
            isValidThorwError(loginErrorMassge,isLogin,loginErrorMassge);
        }
        loginErrorMassge = loginClickHtml.select(".logon-error")[1].text();
        if(StringUtils.isNotBlank(loginErrorMassge)){
            if(loginErrorMassge.contains("访问OBD_P2QuotaInfo失败")){
                loginErrorMassge = "系统繁忙，请稍候再试（登录前）";
            }
            logger.info("ABCBank:taskKey=" + taskKey + "loginErrorMassge:{}",loginErrorMassge);
            isValidThorwError(loginErrorMassge,isLogin,loginErrorMassge);
        }
        logger.info("ABCBank:taskKey=" + taskKey + "loginErrorMassge:{}","登录超时");
        isValidThorwError(loginErrorMassge,isLogin,"登录超时");
    }
    screen_print();
    if(loginSeccessHtml.contains("密码过于简单")){
        lock.unlock();
        logger.info("ABCBank:taskKey=" + taskKey + ",密码设置过于简单，请重置");
        isValidThorwError(loginSeccessHtml.contains("密码过于简单"),isLogin,"密码设置过于简单，请重置");
    }else if (loginSeccessHtml.contains("中国农业银行个人网银首页")) {
        lock.unlock();
        logger.info("ABCBank:taskKey=" + taskKey + ",登陆成功，中国农业银行个人网银首页");
        loginAfterStatus="HASINMAIN";
        logger.info("当前用户无需短信或者其他验证，已经进入主页,不能进行经典版的跳转");
    }else{
        lock.unlock();
        logger.info("ABCBank:taskKey=" + taskKey + ",登陆成功，跳转经典版首页进行抓取");
        // 旧版的页面地址
        var oldUrl = "https://perbank.abchina.com/EbankSite/redirectToOldLogin.do";
        luaFuncs.jumpByUrl(oldUrl)
        // 跳转成功后点击时尚版进入到时尚版页面进行抓取
        // $(".logo03")[0].getElementsByTagName("span")[0].getElementsByTagName("a")[0].click()
        // https://perbank.abchina.com/EbankSite/autoLogin.do
        var fashionUrl = luaFuncs.click_fashion_button();
        logger.info("ABCBank:taskKey={},登陆成功，跳转经典版首页后跳转时尚版的Url：{}",taskKey,fashionUrl);
        if(fashionUrl.contains("https://perbank.abchina.com/EbankSite/autoLogin.do")){
            loginAfterStatus="HASINMAIN";
        }
    }
    hide();
}

/**
 * 判断页面是否加载完全，如果不是则刷新页面三次
 *
 * @param client
 * @param Id
 * @return
 * @throws InterruptedException
 */
function  checkElementIsExist(Id)  {
    var flag = is_exist_by_id(Id);
    for (var i = 1; i < 4; i++) {
        if (flag) {
            break;
        }
        logger.info("ABCBank:taskKey=" + context.get(LUA_TASK_KEY) + "页面指定ID不存在，第" + i + "次刷新页面");
        refresh();
        Thread.sleep(3000);
        var html = luaFuncs.getHtmlGb2312();
        storeDataToFile(context,html, i + "_loginHtml.html");
        flag = is_exist_by_id(Id);
    }
    return flag;
}

/**
 * 检查验证码图片是否存在
 */
function checkValidateImg(codeId) {
    for (var i=0;i<3;i++){
        logger.info("abcbank taskKey ={} 判断验证码图片是否存在！",context.get(LUA_TASK_KEY));
        var exist_by_id_flag=get_image_by_id(codeId);
        if(!exist_by_id_flag.contains("fail")){
            logger.info("abcbank taskKey ={}  图片验证码已经存在，继续输入",context.get(LUA_TASK_KEY));
            return;
        }else {
            //Thread.sleep(1500); //这里可以使用lua的sleep方法
            luaFuncs.abcRefresh();
            Thread.sleep(2000);
        }
    }
}
/**
 * 检查登录的元素是否合法
 * @param password
 * @param loginFlag
 */
function checkLoginTextLegal(password,flag) {

    var patter = "[`~!@#$^&*()=|{}':;',\\[\\].<>/?~！@#￥……&*（）——|{}【】‘’；：”“'。，、？%+\\\\·]";
    //经验证，用户名长度最少为两位
    if (equals("username",flag)) {
        if (password.length < 2) {
            logger.info("当前用户名长度不够，其长度为{}", password.length);
            lock.unlock();
            throw new BankException(BankConstants.BALO_22001_10, "用户名长度不够，停止输入");
        } else if (password.match(patter)) {
            logger.info("用户名包含不合法字符，用户名为{}", password);
            lock.unlock();
            throw new BankException(BankConstants.BALO_22001_10, "用户名包含不合法字符，停止输入");
        }
    }
    if (equals("password",flag)){
        if (password.length<6){
            logger.info("当前密码长度不够，其长度为{}",password.length);
            lock.unlock();
            throw new BankException(BankConstants.BALO_22001_10, "用户密码长度不够，停止输入");
        }else if (password.match(patter)){
            logger.info("abc taskKey = {} 当前用户含有非法字符，停止抓取",context.get(LUA_TASK_KEY));
            lock.unlock();
            throw new BankException(BankConstants.BALO_22001_10, "用户密码包含非法字符，停止输入");
        }
    }
}
/**
 * 检查密码输入三次，如果没有输入进入三次重试
 */
function checkPassCompleteInThree(username,password) {
    var taskKey=context.get(LUA_TASK_TOKEN);
    var status="OK";
    for(var i=1;i<4;i++){
        logger.info("农业银行 taskKey={}开始输入用户名和密码",taskKey);
        show();
        status=luaFuncs.loginUsernameAndPassowrd(username, password, taskKey);
        hide();
        logger.info("农业银行taskKey={}第{}次输入密码返回的状态码为{}",taskKey,i,status);
        if(equals("OK",status)){
            return status;
        }else{
            logger.info("农业银行taskKey={}第{}次输入密码不完整,需要重新输入",taskKey,i,status);
            var aa=luaFuncs.abcRefresh();
        }
    }
    logger.info("农业银行 taskKey= {} 输入三次之后返回的状态为{}",taskKey,status);
    return status;
}

/**
 * 登录成功之后,先进行判断，然后跳转到经典版网银系统，此方法可以避免输入短信验证码或者K宝等验证信息
 */
function jumpRightToClassicalEdition(loginResult) {
    var taskKey = context.get(LUA_TASK_KEY);
    logger.info("abc taskKey = {}  登录成功，释放进程锁" ,taskKey);
    lock.unlock();
    var loginAfterHtml = get_html();
    storeDataToFile(context, loginAfterHtml, "loginAfterHtml.html");
    var passwordFlag = loginAfterHtml.contains("密码过于简单");
    if (passwordFlag) {
        logger.info("ABCBank:taskKey=" + taskKey + "密码设置过于简单，请重置");
        loginResult.errorMsg = "密码设置过于简单，请重置"
        loginResult.statuts  = "SIMPLEPASSWORD"
        return
    }
    logger.info("判断当前窗口是否具有悬浮窗体");
    var frameStatus = luaFuncs.wipeSuspensionframe();
    logger.info("判断窗体返回的状态值为{}", frameStatus);
    if (equals("EMPTYFRAME",frameStatus)) {
        logger.info("当前窗口没有悬浮窗体，继续抓取");
    } else if (equals("FRAMECLICK",frameStatus)) {
        logger.info("当前窗口具有悬浮窗体，执行js已经点击，执行截图操作验证");
        var screenShots = luaFuncs.getScreenShots();
        logger.info("点击悬浮窗体后的截图路径为{}", screenShots);
    } else {
        logger.info("执行lua出错，或者执行js出错");
    }
    //TODO 这里如果为主页，即不需要短信验证码的时候，跳转会出错，这里需要坐下判断，如果为主页，不进行跳转，直接在当前页面进行抓取
    if (loginAfterHtml.contains("中国农业银行个人网银首页")) {
        logger.info("当前用户无需短信或者其他验证，已经进入主页,不能进行经典版的跳转");
        loginAfterStatus="HASINMAIN";
        return ;
    }
    logger.info("ABCBank:taskKey=" +taskKey+ "跳转到旧版网页");
    var oldUrl = "https://perbank.abchina.com/EbankSite/redirectToOldLogin.do";
    luaFuncs.jumpByUrl(oldUrl);
    var scrrenShotsPath = luaFuncs.getScreenShots();
    logger.info("ABCBank: taskKey = " + taskKey+ "跳转到旧版网页的状态截屏路径为" + scrrenShotsPath);
    var flag = judgeAndGetRightHtml("menuTemplates", oldUrl,taskKey);
    if (!flag) {
        logger.info("ABCBank:taskKey=" + taskKey+ "跳转到旧版网页出错，停止抓取");
        loginResult.errorMsg = "页面跳转超时"
        loginResult.statuts  = "TIMEOUT"
        return
    }
}
/**
 * 根据id判断是否进入到正确的页面
 * @param id
 * @param url
 * @param taskKey
 */
function judgeAndGetRightHtml(idss,url,taskKey) {
    var IdN=idss;
    var existFlag = is_exist_by_id(IdN);
    for (var i = 0; i < 3; i++) {
        if (existFlag) {
            return true;
        }
        Thread.sleep(3000);
        logger.info("ABCBank:taskKey=" + taskKey + (i + 1) + "次刷新当前页面");
        refresh();
        Thread.sleep(2000);
        var screentShotsPath = luaFuncs.getScreenShots();
        logger.info("ABCBank: taskKey  =" +taskKey + (i + 1) + "次屏幕截图的路径为" + screentShotsPath);
        existFlag = is_exist_by_id(IdN);
        // existFlag=true;
    }
    return true;
}

/**
 * 时尚版 网银短信登录   如果网银改版，则需要当前的短信验证
 * @return
 * @throws Exception
 */
function inputSmsOnNewEdition(){
    logger.info("用户名密码正确，登录成功，释放进程锁，判断当前页面状态");
    lock.unlock();
    var taskKey = context.get(LUA_TASK_KEY);
    var currentHtml = get_html();
    storeDataToFile(context, currentHtml, "loginAfterHtml.html");
    var checkLoginAfterStatus = checkLoginAfterStatus(currentHtml);
    if (!checkLoginAfterStatus) {//如果返回为true，代表需要短信验证，如果返回为false，证明不需要短信验证码
        return true;
    }
    logger.info("点击发送短信按钮");
    click_by_id("dynamicPswText_sendSms");//这里有弹窗
    var screenShots = luaFuncs.getScreenShots();
    logger.info("发送短信验证码后的截图路径为{}",screenShots); //
    logger.info("SHANGHAIBank:taskKey="+taskKey+"获取验证码");
    //TODO 这里需要取获取短信验证码
    var vcode = waitSmsCodeBySeconds(120);
    logger.info("设置当前短信为{}",vcode);
    set_input_text_by_id_event("dynamicPswText", vcode);
    logger.info("点击提交短信验证码按键"); //这里可以优化 在脚本中使用waittingGoTo的方法 省去等待固定的时间  --已经优化
    luaFuncs.submitSMS("orangeBtn");
    logger.info("首先截图，然后验证当前页面是否登录成功");
    var screenShots2 = luaFuncs.getScreenShots();
    logger.info("点击短信提交后页面截图为{}",screenShots2);
    var flag=this.checkCurrentSms();
    return flag;
}
/**
 * 检查登录后的页面是否正常
 * @param loginAfterHtml 登录后的页面
 * @return 返回true表明需要短信验证码，返回false表明不需要
 */
function checkLoginAfterStatus(loginAfterHtml){
    var flag=true;
    var contains = loginAfterHtml.contains("用户名登录-短信验证");
    if (contains) {
        logger.info("当前用户需要短信验证");
        return true;
    }
    flag=loginAfterHtml.contains("用户名登录-认证安全介质");
    if (flag) {
        logger.info("当前用户需要使用K宝\\K令进行验证");
        throw new BankException(BankConstants.BALO_22102_10, "当前用户需要K宝\\k令进行验证，结束抓取");
    }

    var passwordFlag = loginAfterHtml.contains("密码过于简单");
    if (passwordFlag) {
        logger.info("ABCBank:taskKey=" + context.get(LUA_TASK_KEY) + "密码设置过于简单，请重置");
        throw new BankException(BankConstants.BALO_22003_10, "密码过于简单，需要重置密码");
    }
    logger.info("判断当前窗口是否具有悬浮窗体");
    var frameStatus = luaFuncs.wipeSuspensionframe();
    logger.info("判断窗体返回的状态值为{}", frameStatus);
    if (equals("EMPTYFRAME",frameStatus)) {
        logger.info("当前窗口没有悬浮窗体，继续抓取");
    } else if (equals("FRAMECLICK",frameStatus)) {
        logger.info("当前窗口具有悬浮窗体，执行js已经点击，执行截图操作验证");
        var screenShots = luaFuncs.getScreenShots();
        logger.info("点击悬浮窗体后的截图路径为{}", screenShots);
        return false;
    } else {
        logger.info("执行lua出错，或者执行js出错");
    }
    //TODO 这里如果为主页，即不需要短信验证码的时候，跳转会出错，这里需要坐下判断，如果为主页，不进行跳转，直接在当前页面进行抓取
    if (loginAfterHtml.contains("中国农业银行个人网银首页")) {
        logger.info("当前用户无需短信或者其他验证，已经进入主页,不能进行经典版的跳转");
        loginAfterStatus="HASINMAIN";
        return  false;
    }
    return flag;
}
/**
 * 短信验证码提交后验证短信是否成功
 *  情况1：短信错误  --页面跳转
 *  情况2：短信格式错误  -页面未跳转
 * @throws BankException
 */
function checkCurrentSms(){
    var flag=true;
    var currentHtml = get_html();
    var document = Jsoup.parse(currentHtml);
    if (currentHtml.contains("个人网上银行-错误页面")) {
        logger.info("短信验证失败，停止抓取");
        var elements = document.select(".m-register-yes > div:nth-child(1) > ul:nth-child(2) > li:nth-child(2)");
        if (!elements.isEmpty()) {
            logger.info("验证码错误的原因为{}",elements.get(0).text());
            return false;
        }
    }
    if (currentHtml.contains("短信验证")) {
        logger.info("当前页面未跳转，可能原因为短信格式不对");
        var elements = document.select("#dynamicPswText_sendSms_errMsg");
        if (!elements.isEmpty()) {
            logger.info("验证码错误的原因为{}",elements.get(0).text());
            return false;
        }
    }
    var mainFlag = is_exist_by_id("menuNav");
    if (mainFlag) {
        logger.info("进入到当前账户的主页面,登录成功");
    }else {
        logger.info("未进入主页面,原因未知");
        throw new BankException(BankConstants.BACR_99601_60,"主页面指定id不存在，原因未知");
    }
    return flag;

}
/**
 * 在时尚版抓取借记卡账户信息
 * @param taskkey
 * @throws Exception
 */
function crawlDepositeCardsOnNewEdition(taskkey) {
    logger.info("抓取该账户在时尚版的借记卡信息");
    var list=new ArrayList();
    context.put("debit_data", list);
    var indexUrl = "https://perbank.abchina.com/EbankSite/index.do";
    //获取账户的基本信息
    var indexHtml = ajax_get(indexUrl);
    storeDataToFile(context, indexHtml, "index_fashionEdition.html");
    var cardInfo = RegexUtils.GetText(indexHtml, " var cardInfo = JSON.parse\\(\\'*(.*)").replaceAll(";", "").replaceAll("\'", "").replaceAll("\\)", "");
    var listJsonObject = null;
    try {
        listJsonObject = JSON.toJavaObject(JSON.parseArray(cardInfo), List.class);
    } catch (e) {
        // TODO: handle exception
        logger.info("abcbank taskKey = {}  解析当前页面inddex.html失败,错误原因{}",taskkey,e);
    }
    if (listJsonObject == null || listJsonObject.size() == 0) {
        logger.info("ABCBank:taskKey=" + taskkey + "获取账户信息失败，停止抓取");
        return;
    }
    //获取卡列表
    var card_num = null;
    for(var i=0;i<listJsonObject.size();i++){
        var jsonObject = listJsonObject.get(i);
        var card_type = jsonObject.getString("card_type");
        if (equals("1",card_type)) {
            card_num = jsonObject.getString("card_no");
            if (card_num.length() > 0) {
                logger.info("当前账户有借记卡账户信息");
            } else {
                logger.info("当前账户无信用卡账户信息,结束抓取");
                return;
            }
        }
    }
    //AcctId=6228480018567566270&AcctType=401&AcctCurrCode=156&AcctOofFlag=0
    logger.info("查询该账户下借记卡的信息");
    var deposit_cardInfoHtml = ajax_get("https://perbank.abchina.com/EbankSite/MyAccountInitAct.do");
    storeDataToFile(context, deposit_cardInfoHtml, "deposit_cardInfo.html");
    //String deposit_cardInfo=RegexUtils.GetText(deposit_cardInfoHtml, " var cardInfo = JSON.parse\\(\\'*(.*)");
    var split = deposit_cardInfoHtml.split("new AccViewModel(");
    var split2 = split[1].split("));");
    //String info=split2[0].substring(split2[0].indexOf("{"), split2[0].indexOf("}")+1);
    var info=split2[0].trim().substring(1, split2[0].indexOf("}]',")).trim();
    info=info.substring(0, info.length()-2);
    //JSONArray array = (JSONArray) JSONArray.parse(info);
    var basicLsit=null;
    try {
        basicLsit=JSON.toJavaObject(JSON.parseArray(info), List.class);
    } catch (e) {
        // TODO: handle exception
        logger.error("借记卡账户基本信息解析失败，停止抓取,错误原因为{}",e);
    }
    if (basicLsit==null|| basicLsit.size()==0 ) {
        logger.info("借记卡账户基本信息为空，停止抓取借记卡");
        return;
    }
    logger.info("抓取 到的借记卡的为{}张",basicLsit.size());
    for(var j=0;j<basicLsit.size();j++){
        logger.info("开始抓取第{}张借记卡",(j+1));
        depositCardNum=j+1;
        var creditCardAllData=new CreditCardAllData();
        creditCardAllData.setBankName("农业银行");
        creditCardAllData.setCardId(UUID.randomUUID().toString());
        creditCardAllData.setCashBalance(null);
        creditCardAllData.setCashLimit(null);
        creditCardAllData.setCreditLimit(null);
        var basicInfoJsonObject = basicLsit.get(j);
        creditCardAllData.setNameOnCard(basicInfoJsonObject.getString("acctName"));
        creditCardAllData.setOpenBank(basicInfoJsonObject.getString("branchName"));
        creditCardAllData.setCardType("借记卡");
        var full_card_num=basicInfoJsonObject.getString("acctNo");
        // 处理卡号，农业卡号有的包含“-”字符，44-438100460050107
        if (full_card_num!==null &&full_card_num.length()>4) {
            full_card_num = full_card_num.replace(/(^\s+)|(\s+$)/g,"");// 去除所有空格
            if(full_card_num.contains("-") || full_card_num.contains("")){
                logger.info("农业银行，任务【{}】，卡号{}包含'-'",taskkey,full_card_num);
                full_card_num = full_card_num.replaceAll("-","");
            }
            creditCardAllData.setFullCardNum(full_card_num);
            creditCardAllData.setCardNum(full_card_num.substring(full_card_num.length()-4,full_card_num.length()));
        }
        logger.info("当前卡的：{}的币种为{}",full_card_num,basicInfoJsonObject.getString("currName"));
        var param=new HashMap();
        param.put("acctId", basicInfoJsonObject.getString("acctNo"));
        param.put("acctOpenBankId", basicInfoJsonObject.getString("branchNo"));
        param.put("acctType", basicInfoJsonObject.getString("acctType"));
        param.put("provCode", basicInfoJsonObject.getString("provCode"));
        param.put("acctCurCode", basicInfoJsonObject.getString("currCode"));
        param.put("oofeFlg", basicInfoJsonObject.getString("oofFlag"));
        //存款信息
        crawlDepsoitInfoOnNewEdition(param, creditCardAllData);
        //账单信息  循环抓取六个月的账单信息
        var calendar=Calendar.getInstance();
        var currentMonth = calendar.get(Calendar.MONTH) + 1;
        for(var i=0;i<6;i++){
            var month = calendar.get(Calendar.MONTH) + 1;
            var year=calendar.get(Calendar.YEAR);
            Thread.sleep(1000);
            logger.info("ABCBank:taskey=" + taskkey + "获取借记卡" +year+ "-"+month +"月至前六个月的的详情账单");
            crawlDepsoitBillOnNewEdition(basicInfoJsonObject, creditCardAllData,calendar,currentMonth);
        }
        //根据要求对一些必须的字段进行补全
        checkDepositMonthsRight(creditCardAllData);
        logger.info("抓到的帐单总共有{}个",creditCardAllData.getBills().size());
        list.add(creditCardAllData);
    }

}
/**
 * 时尚版抓取借记卡存款等信息
 * @param param
 * @param creditCardAllData
 */
function crawlDepsoitInfoOnNewEdition(param,creditCardAllData){
    logger.info("抓取当前借记卡的{}的可用余额",param.get("acctId"));
    var map=new LinkedHashMap();
    map.put("multiAcctsString", param.get("acctId"));
    var balanceUrl="https://perbank.abchina.com/EbankSite/AcctBalanceQryAct.do";
    //[{"acctNo":"6228480018567566270","currBalance":"0.10","currUseBalance":"0.10","acctType":"401","acctState":"正常"}]
    var balanceJson = ajax_post(balanceUrl, map);
    storeDataToFile(context, balanceJson, depositCardNum+"_balanceJson.html");
    try {
        var list=JSON.toJavaObject(JSON.parseArray(balanceJson), List.class);
        if (list!==null&&list.size()>0) {
            for(var i=0;i<list.size();i++){
                var listJson = list.get(i);
                var balance = listJson.getString("currBalance");
                creditCardAllData.setBalance(CreditResultUtil.StringToBigDecimal(balance));
            }
        }
    } catch (e) {
        logger.info("解析当前借记卡号{}的账户额度信息失败,错误信息为",param.get("acctId"),e);
    }
    logger.info("抓取当前卡的{}的存款信息",param.get("acctId"));
    var url="https://perbank.abchina.com/EbankSite/SubAcctCurrQueryAct.do";
    //参数信息为 AcctId=卡号   AcctType= AcctCurrCode= AcctOofFlag
    var postDates=new LinkedHashMap();
    postDates.put("AcctId", param.get("acctId"));
    postDates.put("AcctType", param.get("acctType"));
    postDates.put("AcctCurrCode", param.get("currCode"));
    postDates.put("AcctOofFlag", param.get("oofFlag"));
    var depositeInfo = ajax_post(url, postDates);
    storeDataToFile(context, depositeInfo, depositCardNum+"_depositInfo.html");
    var accJsonArray=null;
    try {
        accJsonArray = JSON.parseObject(depositeInfo).getJSONArray("table");
    } catch (e) {
        logger.error("时尚版借记卡解析存款等信息失败，错误原因为{}",e.getMessage());
    }
    if (accJsonArray==null||accJsonArray.size()==0) {
        logger.info("当前卡:{}，无存款信息，停止解析改卡的存款信息，接续抓取",param.get(""));
        return;
    }
    logger.info("解析当前卡:{}借记卡的存款信息",param.get("acctId"));
    for(var i=0;i<accJsonArray.size();i++){
        var accJsonObject = accJsonArray.getJSONObject(i);
        logger.info("解析当前卡借记卡的存款信息accJsonObject:{}",accJsonObject);
        var deposit=new CreditCardTimeDeposit();
        try {
            deposit.setBalance(CreditResultUtil.StringToBigDecimal(accJsonObject.getString("余额")));
            logger.info("当前卡号{}的存款币种为{}",param.get("acctId"),accJsonObject.getString("币种名"));
            deposit.setCurrencyType(CreditResultUtil.convertCurr(accJsonObject.getString("币种名")));
            deposit.setDepositDate(yyyy_MM_dd_PARSER.parse(accJsonObject.getString("开户日期处理")));
            deposit.setDepositType(accJsonObject.getString("账户类型名"));
            var duedatessss = accJsonObject.getString("到期日期处理");//为了与可能的变量名称区别开来
            // duedatessss=duedatessss.replaceAll("\\","")
            if(!duedatessss.constructor("\\")){
                deposit.setDueDate(yyyy_MM_dd_PARSER.parse(duedatessss));
            }
            // deposit.setDueDate(yyyy_MM_dd_PARSER.parse(jsonObject.getString("到期日期处理")));
            deposit.setInterest(CreditResultUtil.StringToBigDecimal(accJsonObject.getString("年利率")).divide(new BigDecimal("100")));
            var periodInfo=accJsonObject.getString("存期处理");
            if (periodInfo.contains("\\")) {
                deposit.setPeriod(null);
                deposit.setPeriodUnit("");
                deposit.setDueDate(null);
            }else{
                setDepositPeriod(periodInfo,deposit);
            }
            creditCardAllData.getDeposits().add(deposit);
        } catch (e) {
            statCaughtException(context, e);
            logger.info("abc bank task = {} 解析借记卡存款信息失败，失败原因 {}", e.message);
        }

    }
}


/**
 * 将汉字形式的数字转化为阿拉伯数字 十二-->
 *
 * @param chinaNum
 *            十二 五
 * @return 12 5
 */
function conbertArab(chinaNum) {
    var convert = "";
    if (chinaNum.length() == 1) {
        convert = basicConvert(chinaNum);
        return convert;
    }
    if (chinaNum.length() == 2) {
        var charArray = chinaNum.toCharArray();
        if (charArray[0] == '十') {
            charArray[0] = '一';
        }
        if (charArray[1] == '十') {
            charArray[1] = '零';
        }
        chinaNum = new String(charArray);
    }
    if (chinaNum.length() == 3) {
        chinaNum = chinaNum.replace("十", "");
    }
    for (var i = 0; i < chinaNum.length; i++) {
        var s = chinaNum.substring(i, i + 1);
        s = basicConvert(s);
        convert = convert + s;
    }
    return convert;
}

/**
 * 借记卡 基本转换
 *
 * @param num
 * @return
 */
function basicConvert(num) {
    var intNum = "";
    if ("一".equals(num)) {
        intNum = "1";
    } else if ("二".equals(num)) {
        intNum = "2";
    } else if ("三".equals(num)) {
        intNum = "3";
    } else if ("四".equals(num)) {
        intNum = "4";
    } else if ("五".equals(num)) {
        intNum = "5";
    } else if ("六".equals(num)) {
        intNum = "6";
    } else if ("七".equals(num)) {
        intNum = "7";
    } else if ("八".equals(num)) {
        intNum = "8";
    } else if ("九".equals(num)) {
        intNum = "9";
    } else if ("十".equals(num)) {
        intNum = "10";
    } else if ("零".equals(num)) {
        intNum = "0";
    }
    return intNum;
}

function addDueDate( flag,  period,  desopoitDate) {
    var yearMonthDay = abcDateConvertString(desopoitDate,"yyyy-MM-dd").split("-");
    var due_date = "";
    if ("YEAR".equals(flag)) {
        var newYear = Integer.parseInt(yearMonthDay[0]) + period;
        due_date = newYear + "-" + yearMonthDay[1] + "-" + yearMonthDay[2];
    }
    if ("MONTH".equals(flag)) {
        var newMonth = Integer.parseInt(yearMonthDay[1]) + period;
        if (newMonth > 12) {
            newMonth = newMonth - 12;
            var newYear = Integer.parseInt(yearMonthDay[0]) + 1;
            yearMonthDay[0] = Integer.toString(newYear);
        }
        if (newMonth < 10) {
            due_date = yearMonthDay[0] + "-0" + newMonth + "-" + yearMonthDay[2];
        } else {
            due_date = yearMonthDay[0] + "-" + newMonth + "-" + yearMonthDay[2];
        }
    }
    return due_date;
}
/**
 * 调用验证错误提示公用方法
 * @param isValidFlag
 * @param errorDec
 * @param teskKey
 */
function isValidThorwError(result,isValidFlag, errorDec) {
    var isSmsCodeCorrectFunc = function(result) {
        return isValidFlag
    }

    var getSmsCodeErrorMsgFunc = function(result) {
        return errorDec
    }

    assertTrueOrThrowException(result, isSmsCodeCorrectFunc, getSmsCodeErrorMsgFunc)
}

/**
 * 借记卡 根据获取到的 时间来获取 定期的到期时间 设置CreditCardTimeDeposit的due_date period_unit
 * period三个属性
 *
 * @param originalPeriod
 * @param deposit
 * @return
 */
function setDepositPeriod( originalPeriod,  deposit) {
    // Map<String, String>map=new HashMap<>();
    var despoitDate = deposit.getDepositDate();
    if (originalPeriod.contains("月")) {
        deposit.setPeriodUnit("MONTH");
        var index = originalPeriod.indexOf("个");
        var num = originalPeriod.substring(0, index);
        var period = Integer.parseInt(conbertArab(num));
        deposit.setPeriod(period);
        var duedate = addDueDate("MONTH", period, despoitDate);
        deposit.setDueDate(yyyy_MM_dd_PARSER.parse(duedate));
    }
    if (originalPeriod.contains("年")) {
        deposit.setPeriodUnit("YEAR");
        var index = originalPeriod.indexOf("年");
        var num = originalPeriod.substring(0, index);
        var period = Integer.parseInt(conbertArab(num));
        deposit.setPeriod(period);
        var duedate = this.addDueDate("YEAR", period, despoitDate);
        deposit.setDueDate(yyyy_MM_dd_PARSER.parse(duedate));
    }
}
/**
 * 抓取该账户的借机卡信息  -经典版
 *
 * @throws Exception
 */
function crawlDepositCardsInfo(taskkey)  {
    var list=new ArrayList();
    context.put("debit_data", list);
    var basicAccountInfo = getDepositCardBasicAccountInfo(taskkey);
    if (basicAccountInfo.size() == 0) {
        logger.info("ABCBank:taskey=" + taskkey + "获取借记卡账户基本信息为空，停止抓取");
        return;
    }
    logger.info("ABCBank:taskey=" + taskkey + "获取到的借记卡卡为" + basicAccountInfo.size() + "张");
    for (var j = 0; j < basicAccountInfo.size(); j++) {
        var basicInfo = basicAccountInfo.get(j);
        var creditCardAllData = new CreditCardAllData();
        depositCardNum = j + 1;
        //存储借记卡基本的信息
        crawlDepositCardBasicInfo(creditCardAllData,  basicInfo);
        //在这里去请求查询账单页面，保存，看存在外币的情况下的处理
        var currecyList = checkQueryDepositBillCurrecys();
        logger.info("当前借记卡可以查询的账单的币种有{}种",currecyList.size());
        for(var i=1;i<currecyList.size();i++){
            var map = currecyList.get(i);
            logger.info("抓取其他币种为：{}的近12个月的账单", map.get("currecyName"));
            crawlDepsoitBillOtherCurrecy(creditCardAllData,map);
        }
        // 获取账单等详情
        var calendar = Calendar.getInstance();
        var currentMonth = calendar.get(Calendar.MONTH) + 1;
        var bills = creditCardAllData.getBills();
        for (var i = 0; i < 6; i++) {
            // BillAllData billAllData =new BillAllData();
            var month = calendar.get(Calendar.MONTH) + 1;
            var year=calendar.get(Calendar.YEAR);
            Thread.sleep(1000);
            logger.info("ABCBank:taskey=" +taskkey + "获取借记卡" +year+ "-"+month +"月至前六个月的的详情账单");

            crawlDepositBillInfo(calendar, currentMonth, i, bills);
        }
        checkDepositMonthsRight(creditCardAllData);
        logger.info("抓到的帐单总共有{}个",creditCardAllData.getBills().size());
        list.add(creditCardAllData);
    }
}

/**
 * 抓取可能的多个借记卡基本账户信息
 *
 * @return
 */
function getDepositCardBasicAccountInfo(taskkey) {
    var list = new ArrayList();
    logger.info("ABCBank:taskKey=" + taskkey + "获取借记卡账户基本信息");
    var basicHtmlCardList = ajax_get("https://perbank.abchina.com/SelfBank/netBank/zh_CN/AcctQryIndexInitAct.ebf");
    storeDataToFile(context,basicHtmlCardList, depositCardNum + "_deposit_cardList.html");
    var doc = Jsoup.parse(basicHtmlCardList);
    var elements = doc.select("#tblAccountList > tbody:nth-child(1) > tr ");
    if (elements.size() > 0 && elements !== null) {
        for(var i=0;i<elements.size();i++){
            var element= elements.get(i);
            var elementss=element.select("td>input:nth-child(1)");
            if (elementss !== null && elementss.size() > 0) {
                var basicInfo = elementss.attr("value");
                if(basicInfo.contains("-")) {
                    logger.info("农业 taskKye= {} 当前账户{}不在抓取",context.get(LUA_TASK_KEY),basicInfo)
                    continue;
                }
                list.add(basicInfo);
            }
        }
    }
    //noinspection JSAnnotator
    return list;
}

/**
 * 爬取借记卡基本数据信息
 *
 * @param creditCardAllData
 * @param client
 * @throws Exception
 */
function crawlDepositCardBasicInfo(creditCardAllData,  basicInfoStr){
    creditCardAllData.setBankName("农业银行");
    creditCardAllData.setCardType("借记卡");
    creditCardAllData.setCardId(UUID.randomUUID().toString());
    var basicInfo = null;
    try {
        basicInfo = basicInfoStr.split("|");
        this.setBasicInfo(basicInfo);
    } catch (e) {
        logger.info("借记卡解析基本信息失败");
        return;
    }
    // 6228480018567566270|156|0|龙枭|401|30949|北京分行|1|566270
    var fullCardNum = basicInfo[0];
    creditCardAllData.setFullCardNum(fullCardNum);
    if (fullCardNum.length() > 4) {
        creditCardAllData.setCardNum(fullCardNum.substring(fullCardNum.length() - 4, fullCardNum.length()));//
    }
    creditCardAllData.setNameOnCard(basicInfo[3]);
    creditCardAllData.setOpenBank(basicInfo[6]);
    creditCardAllData.setCashBalance(null);
    creditCardAllData.setCashLimit(null);
    creditCardAllData.setCreditLimit(null);
    //请求账户信息页面去获取开户时间
    var url="https://perbank.abchina.com/SelfBank/netBank/zh_CN/AcctInfQryInitAct.do?chkAcctNo="+basicInfoStr+"&random=";
    //加一个随机数 五wei
    var random = (Math.random())*100000;//TODO  这里有问题
    var openTimeInfo = ajax_get(url+random);
    storeDataToFile(context,openTimeInfo, depositCardNum+"_opentime_info.html");
    var parse = Jsoup.parse(openTimeInfo);
    var  opentime ="";
    try {
        opentime=parse.select("#lblOpenTime").get(0).text();
        opentime = opentime.replaceAll("年", "-").replaceAll("月", "-").replaceAll("日", "");
    } catch (e) {
        logger.info("当前信息卡的开户时间获取失败");
    }

    if(opentime.length!==0){
        creditCardAllData.setOpenTime(yyyy_MM_dd_PARSER.parse(opentime));
    }

    //请求账户的存款等信息
    Thread.sleep(2000);
    var accountUrl = "https://perbank.abchina.com/SelfBank/netBank/zh_CN/AcctBalanceQrySingleAct.do?acctOpenBankId="+basicInfo[5]+"&acctId=" +basicInfo[0]+
        "&acctType="+basicInfo[4]+"&oofeFlg="+basicInfo[2]+"&acctCurCode="+basicInfo[1]+"&acctName="+basicInfo[3]+"&acctOpenBankIdName="+basicInfo[6]+"&subacctType=99&subacctStatus=00&random="+basicInfo[8];
    // accountUrl = String.format(accountUrl, basicInfo[5], basicInfo[0], basicInfo[4], basicInfo[2], basicInfo[1],
    //     basicInfo[3], basicInfo[6], basicInfo[8]);
    // 将一些基本信息存入到中
    logger.info("ABCBank:taskKey=" + context.get(Constants.LUA_TASK_KEY) + "获取借记卡账户存款账户基本信息");
    var html = ajax_get(accountUrl);
    storeDataToFile(context,html, depositCardNum + "_deposits_info.html ");
    var balanceHtmlDoc = Jsoup.parse(html);
    // 获取关于账户存款信息方面的信息
    var elementDeposit = balanceHtmlDoc.select("#subAcctList > tbody:nth-child(1)>tr");
    var firsetElement = null;
    try {
        firsetElement = elementDeposit.get(1);
    } catch (e) {
        // 如果出现异常 不在继续解析
        logger.error("ABCBank:taskKey=" + context.get("") + "解析借记卡账户存款信息失败");
        return;
    }
    var openTime = firsetElement.select(" td:nth-child(3)").first().text();
    var balance = 0;
    for (var i = 1; i < elementDeposit.size(); i++) {
        var element = elementDeposit.get(i);
        var balanceStr = element.select("td:nth-child(8)").first().text();
        balance = balance + Double.parseDouble(balanceStr);
    }
    creditCardAllData.setBalance(new BigDecimal(balance).setScale(2, BigDecimal.ROUND_HALF_UP));
    parseDeposits(elementDeposit, creditCardAllData);
}
/**
 * 借记卡 将一些基本的数据信息放入到本地Map中
 *
 * @param basicInfo
 *            6228480018567566270|156|0|龙枭|401|30949|北京分行|1|566270
 */
function setBasicInfo(basicInfo) {
    context.put("acctOpenBankId", basicInfo[5]);
    context.put("acctId", basicInfo[0]);
    context.put("acctType", basicInfo[4]);
    context.put("oofeFlg", basicInfo[2]);
    context.put("acctCurCode", basicInfo[1]);
    context.put("acctName", basicInfo[3]);
    context.put("acctOpenBankIdName", basicInfo[6]);
    context.put("random", basicInfo[8]);
}


/**
 * 借记卡：解析存款等基本信息
 *
 * @param elements
 * @param creditCardAllData
 */
function parseDeposits(elements, creditCardAllData) {
    logger.info("ABCBank:taskKey=" +context.get(LUA_TASK_KEY) + "解析账户借记卡存款信息");
    var deposits = creditCardAllData.getDeposits();
    for (var i = 0; i < elements.size(); i++) {
        if (i == 0) {// 跳过第一次循环
            continue;
        }
        try {
            var element = elements.get(i);
            var creditCardTimeDeposit = new CreditCardTimeDeposit();
            var balance = element.select("td:nth-child(8)").get(0).text();
            var currecy = element.select("td:nth-child(4)").get(0).text();
            var deposit_date = element.select("td:nth-child(3)").get(0).text();
            var insert = element.select(" td:nth-child(7)").get(0).text();
            var type = element.select("td:nth-child(1)").get(0).text();
            creditCardTimeDeposit.setBalance(new BigDecimal(balance));
            creditCardTimeDeposit.setCurrencyType(CreditResultUtil.convertCurr(currecy));
            logger.info("ABC taskKey={}当前借记卡存款的币种为{}",context.get(LUA_TASK_KEY),currecy);
            creditCardTimeDeposit.setDepositDate(yyyy_MM_dd_PARSER.parse(deposit_date));
            // 利率一般为1.35 等，有没有可能为1.356?这种情况下如何选择？
            //var divide = CreditResultUtil.StringToBigDecimal("0.003");
            creditCardTimeDeposit.setInterest(CreditResultUtil.StringToBigDecimal("0.003").setScale(3,BigDecimal.ROUND_HALF_DOWN));
            var interestLog = creditCardTimeDeposit.getInterest();
            logger.info("ABC taskKey = {} 获取到的利率为 {} ",context.get(LUA_TASK_KEY),interestLog)
            if (type.contains("整整")) {
                type = "整存整取";
                // 获取到的是 "三个月" 的形式
                var originalPeriod = element.select("td:nth-child(6)").get(0).text();
                // 设置CreditCardTimeDeposit的due_date period_unit period三个属性
                setDepositPeriod(originalPeriod, creditCardTimeDeposit);
            }
            if (type.contains("活期")) {
                type = "活期储蓄";
                creditCardTimeDeposit.setPeriodUnit("");
                creditCardTimeDeposit.setPeriod(null);
            }
            creditCardTimeDeposit.setDepositType(type);
            deposits.add(creditCardTimeDeposit);
        } catch (e) {
            logger.info("ABCBank:taskKey=" + context.get(LUA_TASK_KEY)+ "解析借记卡账户存款信息出错，继续抓取"+e.message);
            return;
        }

    }
}

/**
 * 请求借记卡的查询页面，获取可以查询的币种
 */
function checkQueryDepositBillCurrecys(){
    logger.info("请求查询借记卡账单的页面，获取可以查询的账单的币种");
    var url="https://perbank.abchina.com/SelfBank/netBank/zh_CN/AcctDetailSubAcctQueryAct.do";
    //AcctId=6228480018567566270&AcctType=401&AcctBranchNo=30949&AcctOofFlag=0&AcctCurrCode=156
    var param=new LinkedHashMap();
    param.put("AcctId", context.get("acctId"));
    param.put("AcctType",  context.get("acctType"));
    param.put("AcctBranchNo",  context.get("acctOpenBankId"));
    param.put("AcctOofFlag",  context.get("oofeFlg"));
    param.put("AcctCurrCode", context.get("acctCurCode"));
    var queryMonth = ajax_post(url, param);
    storeDataToFile(context,queryMonth, depositCardNum+"_queryDepositBill.html");
    var doc = Jsoup.parse(queryMonth);
    var list=new LinkedList();
    var elements = doc.select("#SubAcctCur > option");
    //<option  value=0|156 selected>人民币|本币</option>
    if (!elements.isEmpty()) {
        for(var i=0;i<elements.size();i++){
            var element=elements.get(i)
            var text = element.text();
            var value=element.attr("value");
            logger.info("可以选择的币种有{}，对应的code为{}",text,value);
            var map=new LinkedHashMap();
            var split = value.split("|");
            if (split.length>1) {
                map.put("oofeFlg",split[0]);
                map.put("acctCurCode", split[1]);
                map.put("currecyName", text);
                list.add(map);
            }
        }
    }
    return list;
}

/**
 * 经典版-抓取其他币种的账单
 * @param creditCardAllData
 * @throws Exception
 */
function crawlDepsoitBillOtherCurrecy(creditCardAllData,currecyAndCode) {
    var calendar = Calendar.getInstance();
    var currentMonth = calendar.get(Calendar.MONTH) + 1;
    var bills = creditCardAllData.getBills();
    var month = calendar.get(Calendar.MONTH) + 1;
    var year=calendar.get(Calendar.YEAR);
    logger.info("ABCBank:taskey=" + context.get(LUA_TASK_KEY) + "获取借记卡" +year+ "-"+month +"月至前12个月的的详情账单");
    // bills.add(billAllData);
    var param = new LinkedHashMap();
    var dateFormat=new SimpleDateFormat("yyyyMMdd");
    var startDate=dateFormat.format(calendar.getTime());
    calendar.add(Calendar.MONTH, -12);
    var endDate=dateFormat.format(calendar.getTime());
    param.put("trnStartDt", startDate);// 格式为20170606
    param.put("trnEndDt", startDate);// 格式为20170606
    param.put("acctOpenBankId", context.get("acctOpenBankId"));// 这里有问题
    param.put("acctName",  context.get("acctName"));
    param.put("acctId", context.get("acctId"));
    param.put("acctType", context.get("acctType"));
    param.put("oofeFlg", currecyAndCode.get("oofeFlg"));
    param.put("acctCurCode",  currecyAndCode.get("acctCurCode"));
    param.put("busCode", "200002");
    param.put("sYear", startDate.substring(0, 4));
    param.put("sMonth", startDate.substring(4, 6));
    param.put("sDay", startDate.substring(6, 8));
    param.put("eYear", endDate.substring(0, 4));
    param.put("eMonth", endDate.substring(4, 6));
    param.put("eDay", endDate.substring(6, 8));
    param.put("nowdate", formatDate(startDate));// 格式为2017-06-06
    var url = "https://perbank.abchina.com/SelfBank/netBank/zh_CN/accountDetailDownLoadQryAct.do";
    var datas = "";
    var entrySet = param.entrySet();
    // for(var i=0;i<entrySet.size();i++){
    for each(var entry in entrySet){
        var key = entry.getKey();
        var value = entry.getValue();
        datas = datas + key + "=" + value + "&";
    }
    var path = luaFuncs.ajaxGetFile(url, datas);
    if (equals("EMPTYPATH",path) || path == null || path.length() == 0) {
        logger.info("请求借记卡其他币种12个月数据，请求获取数据失败,停止解析该月");
        return;
    }
    var copyPoth=getCopyPath(context.get(LUA_TASK_KEY));
    luaFuncs.copyFile(path, context.get(LUA_TASK_KEY), depositCardNum + "_otherCurrrecy" +currecyAndCode.get("acctCurCode")  + ".xls",copyPoth);
}
/**
 * 根据taskKey获取复制文件的路径
 * @param taskKey
 * @return
 */
function getCopyPath(taskKey){
    var calendar =Calendar.getInstance();
    var year = calendar.get(Calendar.YEAR);
    var month=calendar.get(calendar.MONTH)+1;
    var day=calendar.get(calendar.DAY_OF_MONTH);
    return "d:\\htmlStorage\\BANK\\"+year+"\\"+month+"\\"+day+"\\ABC\\"+taskKey;
}
/**
 * 获取借记卡账单等信息 -六个月
 *
 * @param calendar
 * @param client
 * @throws Exception
 */
function crawlDepositBillInfo( calendar, currentMonth, i, bills) {
    var billAllData = new BillAllData();
    var taskkey=context.get(LUA_TASK_KEY);
    var url = "https://perbank.abchina.com/SelfBank/netBank/zh_CN/accountDetailDownLoadQryAct.do";
    var param = new LinkedHashMap();
    var startAndEndDate=get6MonthStartAndEnd(currentMonth, calendar);
    param.put("trnStartDt", startAndEndDate[0]);// 格式为20170606
    param.put("trnEndDt", startAndEndDate[1]);// 格式为20170606
    param.put("acctOpenBankId", context.get("acctOpenBankId"));// 这里有问题
    param.put("acctName", context.get("acctName"));
    param.put("acctId", context.get("acctId"));
    param.put("acctType", context.get("acctType"));
    param.put("oofeFlg", context.get("oofeFlg"));
    param.put("acctCurCode", context.get("acctCurCode"));
    param.put("busCode", "200002");
    param.put("sYear", startAndEndDate[0].substring(0, 4));
    param.put("sMonth", startAndEndDate[0].substring(4, 6));
    param.put("sDay", startAndEndDate[0].substring(6, 8));
    param.put("eYear", startAndEndDate[1].substring(0, 4));
    param.put("eMonth", startAndEndDate[1].substring(4, 6));
    param.put("eDay", startAndEndDate[1].substring(6, 8));
    param.put("nowdate", formatDate(startAndEndDate[1]));// 格式为2017-06-06
    var billdate = startAndEndDate[0].substring(0, 4) + "-" + startAndEndDate[0].substring(4, 6) + "-01";
    var bill_month = formatMonth(startAndEndDate[0]);
    logger.info("抓取借记卡" + bill_month + "月所有数据，请求下载文件");
    var datas = "";

    var entrySet = param.entrySet();
    for each(var entry in entrySet){
        var key = entry.getKey();
        var value = entry.getValue();
        datas = datas + key + "=" + value + "&";
    }
    var path = luaFuncs.ajaxGetFile(url, datas);
    if (equals("EMPTYPATH",path) || path == null || path.length() == 0) {
        logger.info("请求借记卡" + bill_month + "月数据，请求获取数据失败,停止解析该月");
        return;
    }
    //根据任务号获取复制的路径
    var copyPoth=getCopyPath(taskkey);
    //将文件挪到页面保存的路径中去
    luaFuncs.copyFile(path, taskkey, depositCardNum + "_" + bill_month + ".xls",copyPoth);
    try {
        //这里暂定为使用本类中的readExcel的方法
        var allbills = read2003Excel(new File(path));
        if (allbills == null || allbills.size() < 4) {
            logger.info("抓取借记卡" + bill_month + "月数据无数据，停止解析");
            return;
        }
        billAllData.setBankName("农业银行");
        billAllData.setBillId(UUID.randomUUID().toString());
        billAllData.setBillType("");
        logger.info("解析借记卡" + bill_month + "月数据");
        parseDepositBillExcel(allbills, billAllData,bills);
        var size = billAllData.getShoppingSheets().size();
        if (size == 0) {
            logger.info("抓取借记卡" + bill_month + "月数据无数据");
        }
    } catch (e) {
        logger.info("解析借记卡" + bill_month + "月数据失败，停止解析,错误原因为：{}",e.getMessage());
    }
}

/**
 * @param date Date类型的
 * @return string 类型的日期 'yyyy-MM-dd'
 */
function abcDateConvertString(date,patter) {
    var dateFormat = new SimpleDateFormat(patter);
    if(date == null ) {
        return "";
    }
    var format = dateFormat.format(date.getTime());
    return format;
}


/**
 * 根据当前账单中所有已经有的账单和每个月对比，如果没有，就添加到账单中
 * @param creditCardAllData
 */
function checkDepositMonthsRight(creditCardAllData){
    var bills = creditCardAllData.getBills();
    var allmonth=Calendar.getInstance();
    var open_time = creditCardAllData.getOpenTime();

    var openTimeStr = abcDateConvertString(open_time, "yyyy-MM-dd");

    for(var i=0;i<36;i++){
        var format=new SimpleDateFormat("yyyy-MM");
        var billMonth = format.format(allmonth.getTime());
        var flag="NO";
        for(var j=0;j<bills.size();j++){
            var billAllData=bills.get(j);
            var bill_month = billAllData.getBillMonth();
            if (bill_month.equals(billMonth)) {
                logger.info("当前{}月账单已经抓取到",bill_month);
                flag="HAVE";
            }
        }
        if (equals("NO",flag)) {
            logger.info("该月{}账单中没有，向其中添加该月表头",billMonth);
            var  billAllData=new BillAllData();
            billAllData.setBankName("农业银行");
            billAllData.setBillDate(yyyy_MM_dd_PARSER.parse(billMonth+"-01"));
            billAllData.setBillMonth(billMonth);
            billAllData.setBillType("");
            billAllData.setNewBalance(null);
            billAllData.setUsdNewBalance(null);
            billAllData.setMinPayment(null);
            billAllData.setUsdMinPayment(null);
            billAllData.setMinPayment(null);
            billAllData.setLastBalance(null);
            billAllData.setUsdLastBalance(null);
            billAllData.setLastPayment(null);
            billAllData.setUsdLastPayment(null);
            billAllData.setNewCharges(null);
            billAllData.setUsdNewCharges(null);
            billAllData.setInterest(null);
            billAllData.setUsdInterest(null);
            billAllData.setCreditLimit(null);
            billAllData.setCashLimit(null);
            billAllData.setUsdCashLimit(null);
            billAllData.setUsdCreditLimit(null);
            bills.add(billAllData);
        }
        if (openTimeStr.length>=7) {//取开户时间，如果当前月等于开户时间，那么不进行下一次循环
            var open_month=openTimeStr.substring(0,7);
            if (open_month.equals(billMonth)) {
                logger.info("当前卡的开户时间为{}，停止继续填充表头",openTimeStr);
                break;
            }
        }
        allmonth.add(Calendar.MONTH, -1);

    }
}


/**
 * 借记卡 规范日期返回日期
 *
 * @param dateStr
 *            formatDate('20170507')
 * @return 2017-05-07
 */
function formatDate(dateStr) {
    if (dateStr==null || equals("",dateStr)) {
        return "";
    }
    dateStr = RegexUtils.GetText(dateStr, "(\\d+)");
    var d = null;
    try {
        d = DateUtils.parseDate(dateStr, "yyyyMMdd");
    } catch (e) {
        logger.error("解析日期格式出错" + dateStr, e);
    }
    dateStr = DateFormatUtils.format(d, "yyyy-MM-dd");
    return dateStr;
}
/**
 * 借记卡 返回月份
 *
 * @param dateStr
 *            formatDate('20170507')
 * @return 2017-05-07
 */
function formatMonth(dateStr) {
    dateStr = RegexUtils.GetText(dateStr, "(\\d+)");
    var d = null;
    try {
        d = DateUtils.parseDate(dateStr, "yyyyMMdd");
    } catch (e) {
        logger.error("解析日期格式出错" + dateStr, e);
    }
    dateStr = DateFormatUtils.format(d, "yyyy-MM");
    return dateStr;
}

/**
 * 读取 office 2003 excel
 *
 * @throws IOException
 * @throws FileNotFoundException
 */
function read2003Excel(file)  {
    var list = new LinkedList();
    var hwb = new HSSFWorkbook(new FileInputStream(file));
    var sheet = hwb.getSheetAt(0);
    var value = null;
    var row = null;
    var cell = null;
    var counter = 0;
    for (var i = sheet.getFirstRowNum(); counter < sheet.getPhysicalNumberOfRows(); i++) {
        row = sheet.getRow(i);
        if (row == null) {
            continue;
        } else {
            counter++;
        }
        var linked = new LinkedList();
        for (var j = row.getFirstCellNum(); j <= row.getLastCellNum(); j++) {
            cell = row.getCell(j);
            if (cell == null) {
                linked.add("");
            } else {
                linked.add(cell.toString());
            }

        }
        list.add(linked);
    }
    return list;
}

/**
 * 获取留个月内的开始时间和结束时间
 */
function get6MonthStartAndEnd(currentMonth,calendar){
    var startAndEnd=new Array(2);
    var monthStr="";
    //String dayStr="";
    var endMonthStr="";
    var endDay="";
    var endMonth = calendar.get(Calendar.MONTH)+1;
    var endYear=calendar.get(Calendar.YEAR);
    var endDaysss=calendar.get(Calendar.DAY_OF_MONTH);
    //开始时间 20170911    20170301
    var   startMonth=0;
    if (endMonth==currentMonth) {//如果是当前月分
        calendar.add(Calendar.MONTH, -5);
        startMonth = calendar.get(Calendar.MONTH)+1;
        if (endDaysss<10) {
            endDay="0"+endDaysss;
        }else {
            endDay=Integer.toString(endDaysss);
        }
    }else{
        calendar.add(Calendar.MONTH, -5);
        startMonth = calendar.get(Calendar.MONTH)+1;
        endDay="30";
    }
    var year=calendar.get(Calendar.YEAR);
    //int day=calendar.get(Calendar.DAY_OF_MONTH);
    if (startMonth<10) {
        monthStr="0"+startMonth;
    }else{
        monthStr=Integer.toString(startMonth);
    }
    if (endMonth<10) {
        endMonthStr="0"+endMonth;
    }else{
        endMonthStr=Integer.toString(endMonth);
    }
    startAndEnd[0]=year+monthStr+"01";
    startAndEnd[1]=endYear+endMonthStr+endDay;
    calendar.add(Calendar.MONTH, -1);
    return startAndEnd;
}

/**
 * 解析借记卡账单的excel
 *
 * @param bills
 * @param billAllData
 * @param billList
 * @throws IOException
 */
function parseDepositBillExcel( bills, billAllData, billList)  {
    var beforeMonth="";
    if (bills !== null && bills.size() > 0) {
        for (var i = 0; i < bills.size(); i++) {
            if (i < 3) {
                continue;
            }
            var values = bills.get(i);
            if (values !== null && values.size() > 0) {
                var expense = new Expense();
                var post_date = values.get(0).toString();//交易日期
                if (post_date == null || post_date.equals("")) {
                    logger.info("当前行无数据，停止本行填充数据");
                    continue;
                }
                billAllData=checkDetailOnMonth(post_date, beforeMonth, billAllData, billList);
                //shopping_sheets=billAllData.getShoppingSheets();
                beforeMonth=post_date.substring(0,6);//交易月份
                post_date = formatDate(post_date);// 转换日期类型为 yyyy-MM-dd
                var trans_date = post_date + " " + values.get(1).toString().replaceAll("(?<=^\\d{2})", ":")
                    .replaceAll("(?<=^\\d{2}\\:\\d{2})", ":");
                var amountMoney = values.get(3).toString().trim();//交易金额
                if (amountMoney.equals("")) {
                    amountMoney = "-" + values.get(2);
                }
                var opposite_card_no = values.get(5);// 对方账号
                var name_on_opposite_card =  values.get(6);
                var opposite_bank =  values.get(7);
                var trans_method =  values.get(8);
                var trans_channel = values.get(9);
                var description =  values.get(11);
                var remake =  values.get(10);
                var balance =  values.get(4);
                expense.setId(UUID.randomUUID().toString());// 消费记录ID
                var amount_money = Double.parseDouble(amountMoney);//
                expense.setAmountMoney(new BigDecimal(amount_money).setScale(2, BigDecimal.ROUND_HALF_UP));// 消费金额
                expense.setBalance(new BigDecimal(balance));// 余额
                var cardNum = context.get("acctId");
                expense.setCardNum(cardNum.substring(cardNum.length() - 4, cardNum.length()));//卡号后四位
                var shopping_sheets = billAllData.getShoppingSheets();
                var index = shopping_sheets.size() + 1;
                expense.setOrderIndex(index);// 排序
                index++;
                expense.setRemark(remake);// 备注

                var category=CategoryUtil.getDebitCategory(description, amountMoney);

                if (equals("OTHERREVENUE",category)&& amountMoney.contains("-")) {
                    category=CreditResultUtil.judgeDepositBillCategory(trans_method);
                }
                if(null == category || equals(category,"")){
                    category="OTHEREXPENSE";
                }
                expense.setCategory(category);// 消费类型
                expense.setDescription(description);// 描述
                expense.setNameOnOppositeCard(name_on_opposite_card);// 对方持卡人
                expense.setOppositeBank(opposite_bank);// 对方银行
                expense.setOppositeCardNo(opposite_card_no);// 对方账号
                expense.setPostDate(yyyy_MM_dd_PARSER.parse(post_date));// 记账日期
                expense.setTransDate(yyyy_MM_dd_HH_mm_ss_PARSER.parse(post_date+" 00:00:00"));// 交易时间
                expense.setTransChannel(trans_channel);// 交易通道
                expense.setTransMethod(trans_method);// 交易方式
                expense.setCurrencyType("CNY");// 币种
                shopping_sheets.add(expense);
            }
        }
    }
}


/**
 * 根据excel的时间来判断 当前账单分属哪个月
 * @param transDate
 * @param beforeMonth
 * @param billAllData
 * @param bills
 */
function checkDetailOnMonth( transDate, beforeMonth,billAllData,bills){
    var currentMonth="";
    if (transDate !== null && transDate.length()>4) {
        currentMonth=transDate.substring(0,6);
    }
    if (equals(currentMonth,beforeMonth) || equals("",beforeMonth)) {
        billAllData.setBillDate(yyyy_MM_dd_PARSER.parse(formatMonth(transDate)+"-01"));
        billAllData.setBillMonth(formatMonth(transDate));
        billAllData.setBillType("");
        billAllData.setNewBalance(null);
        billAllData.setUsdNewBalance(null);
        billAllData.setMinPayment(null);
        billAllData.setUsdMinPayment(null);
        billAllData.setMinPayment(null);
        billAllData.setLastBalance(null);
        billAllData.setUsdLastBalance(null);
        billAllData.setLastPayment(null);
        billAllData.setUsdLastPayment(null);
        billAllData.setNewCharges(null);
        billAllData.setUsdNewCharges(null);
        billAllData.setInterest(null);
        billAllData.setUsdInterest(null);
        billAllData.setCreditLimit(null);
        billAllData.setCashLimit(null);
        billAllData.setUsdCashLimit(null);
        billAllData.setUsdCreditLimit(null);
        //为避免数据结果序列化的时候 出现多引用的情况，增加如下判断：
        //1.当账单集合的大小大于0的时候，取集合中最后一个账单，取出账单月与当前账单月进行比较，如果相同证明为一个月的账单，如果不同证明为第一次进入这里，为不同月的账单，需要添加到账单集合中
        //2.当账单集合的大小等于0的时候，证明账单列表中没有账单，需要将当前账单加入到集合中
        if (bills.size()>0) {
            var beforeBill = bills.get(bills.size()-1);
            var beforeBillMonth = beforeBill.getBillMonth();
            var currenBillMonth=formatMonth(transDate);
            if(equals(beforeBillMonth,currenBillMonth)){

            }else{
                logger.info("当前月账单列表中没有，需再向账单列表中添加");
                bills.add(billAllData);
            }
        }else{
            bills.add(billAllData);
        }
        return billAllData;
    }else {
        logger.info("当前账单属于{}",currentMonth);
        billAllData=new BillAllData();
        billAllData.setBankName("农业银行");
        billAllData.setBillId(UUID.randomUUID().toString());
        billAllData.setBillType("");
        billAllData.setBillDate(yyyy_MM_dd_PARSER.parse(formatMonth(transDate)+"-01"));
        billAllData.setBillMonth(formatMonth(transDate));
        billAllData.setBillType("");
        billAllData.setNewBalance(null);
        billAllData.setUsdNewBalance(null);
        billAllData.setMinPayment(null);
        billAllData.setUsdMinPayment(null);
        billAllData.setMinPayment(null);
        billAllData.setLastBalance(null);
        billAllData.setUsdLastBalance(null);
        billAllData.setLastPayment(null);
        billAllData.setUsdLastPayment(null);
        billAllData.setNewCharges(null);
        billAllData.setUsdNewCharges(null);
        billAllData.setInterest(null);
        billAllData.setUsdInterest(null);
        billAllData.setCreditLimit(null);
        billAllData.setCashLimit(null);
        billAllData.setUsdCashLimit(null);
        billAllData.setUsdCreditLimit(null);
        bills.add(billAllData);
        //List<Expense> shopping_sheets = billAllData.getShoppingSheets();
        return billAllData;
    }
}

/**
 * 时尚版抓取借记卡账单
 * @param param
 * @param creditCardAllData
 * @throws Exception
 */
function crawlDepsoitBillOnNewEdition(basicInfoJsonObject, creditCardAllData, calendar, currentMonth){
    var downloadUrl="https://perbank.abchina.com/EbankSite/AccountTradeDetailDownloadAct.do";
    //参数信息  acctId=卡号 acctOpenBankId=30949 acctType=401 provCode=11 acctCurCode=156 oofeFlg=0  trnStartDt=20170713  trnEndDt=20171013
    var startAndEndDate=this.get6MonthStartAndEnd(currentMonth, calendar);
    var param = new HashMap();
    param.put("acctCurCode", basicInfoJsonObject.getString("currCode"));
    param.put("acctId", basicInfoJsonObject.getString("acctNo"));
    param.put("acctOpenBankId", basicInfoJsonObject.getString("branchNo"));
    param.put("acctType", basicInfoJsonObject.getString("acctType"));
    param.put("oofeFlg", basicInfoJsonObject.getString("oofFlag"));
    param.put("provCode", basicInfoJsonObject.getString("provCode"));
    param.put("queryMode", "0");
    param.put("trnStartDt", startAndEndDate[0]);// 格式为20170606
    param.put("trnEndDt", startAndEndDate[1]);// 格式为20170606
    //拼接参数
    var datas="";
    var entrySet = param.entrySet();

    // for(var i=0;i<entrySet.size();i++){
    for each(var entry in entrySet){
        var key = entry.getKey();
        var value=entry.getValue();
        datas=datas+key+"="+value+"&";
    }
    //下载文件 返回的文件的路径 默认保存在release的temp文件夹中
    var billPaths = luaFuncs.ajaxGetFile(downloadUrl, datas);
    if (billPaths==null ||billPaths.length()==0||billPaths.equals("EMPTYPATH")) {
        logger.info("当前卡{},请求下载{}至{}月账单失败，路径为空，停止抓取该月",param.get("acctId"),startAndEndDate[0],startAndEndDate[1]);
        return;
    }
    // 复制文件到指定文件夹下
    filePathToLocal(context,billPaths,"借_" + "bill_" + startAndEndDate[0]+"至"+startAndEndDate[1] + ".xls",context.get(LUA_TASK_KEY));
    logger.info("当前借记卡：{}路径为{}",param.get("acctId"),billPaths);
    // var copyPoth=getCopyPath(context.get(LUA_TASK_KEY));
    // //将文件复制到存储页面的路径中
    // luaFuncs.copyFile(billPaths, context.get(LUA_TASK_KEY), depositCardNum + "_" + startAndEndDate[0] + ".xls",copyPoth);
    var bills = creditCardAllData.getBills();
    try {
        var allbills = read2003Excel(new File(billPaths));
        if (allbills == null || allbills.size() < 4) {
            logger.info("抓取借记卡" + startAndEndDate[0]+"至"+startAndEndDate[1] + "月以及之后的六个月数据无数据，停止解析");
            return;
        }
        var billAllData=new BillAllData();
        billAllData.setBankName("农业银行");
        billAllData.setBillId(UUID.randomUUID().toString());
        billAllData.setBillType("");
        logger.info("解析借记卡" + startAndEndDate[0] + "月以及之后的六个月数据");
        //解析excel文件
        parseDepositBillExcelOnNewEdition(allbills, billAllData,bills,param.get("acctId"));
        var size = billAllData.getShoppingSheets().size();
        if (size == 0) {
            logger.info("解析借记卡" + startAndEndDate[0]+"至"+startAndEndDate[1] + "月以及之后的六个月无数据");
        }
    } catch ( e) {
        logger.info("解析借记卡" + startAndEndDate[0]+"至"+startAndEndDate[1] + "月以及之后的六个月数据失败，停止解析，失败原因为{}",e);
    }


}
/**
 * 时尚版的解析借记卡excel
 * @param bills
 * @param billAllData
 * @param billList
 * @param cardId
 *
 */
function parseDepositBillExcelOnNewEdition(bills,billAllData,billList,cardId){
    var beforeMonth="";
    if (bills != null && bills.size() > 0) {

        for (var i = 0; i < bills.size(); i++) {
            if (i < 3) {
                continue;
            }
            var values = bills.get(i);
            if (values != null && values.size() > 0) {
                var expense = new Expense();
                var post_date = values.get(0).toString();
                if (post_date == null || post_date.equals("")) {
                    logger.info("当前行无数据，停止本行填充数据");
                    continue;
                }
                billAllData=this.checkDetailOnMonth(post_date, beforeMonth, billAllData, billList);
                //shopping_sheets=billAllData.getShoppingSheets();
                beforeMonth=post_date.substring(0,6);
                post_date = formatDate(post_date);
                var trans_date = post_date + " " + values.get(1).toString().replaceAll("(?<=^\\d{2})", ":")
                    .replaceAll("(?<=^\\d{2}\\:\\d{2})", ":");
                var amountMoney = values.get(2).toString().trim();

                var opposite_card_no =  values.get(5);
                var name_on_opposite_card = values.get(4);
                var opposite_bank =  values.get(6);
                var trans_method =  values.get(8);
                var trans_channel =  values.get(7);
                var description =  values.get(10);
                var remake =  values.get(9);
                var balance =  values.get(3);
                expense.setId(UUID.randomUUID().toString());
                var amount_money = Double.parseDouble(amountMoney);
                expense.setAmountMoney(new BigDecimal(amount_money).setScale(2, BigDecimal.ROUND_HALF_UP));
                //文档定为支出为正，收入为负，下载中支出为负，收入为正数
                expense.setAmountMoney(expense.getAmountMoney().divide(new BigDecimal("-1")));
                expense.setBalance(new BigDecimal(balance));
                var cardNum = cardId;
                expense.setCardNum(cardNum.substring(cardNum.length() - 4, cardNum.length()));
                var shopping_sheets = billAllData.getShoppingSheets();
                var index = shopping_sheets.size() + 1;
                expense.setOrderIndex(index);
                index++;
                expense.setRemark(remake);
                //使用新的判断消费类型的方法
                if (amountMoney.contains("-")) {
                    amountMoney.replaceAll("-", "");
                }else if (amountMoney.contains("+")) {
                    amountMoney="-"+amountMoney.replaceAll("\\+", "");
                }
                var category=CategoryUtil.getDebitCategory(description, amountMoney);
                if (category.equals("OTHERREVENUE")&& amountMoney.contains("-")) {
                    category=CreditResultUtil.judgeDepositBillCategory(trans_method);
                }
//					if (category.equals("OTHEREXPENSE")&&amountMoney.contains("-")) {
//						category ="OTHERREVENUE";
//					}
                if( null == category || "".equals(category)){
                    category="OTHEREXPENSE";
                }
                expense.setCategory(category);
                expense.setDescription(description);
                expense.setNameOnOppositeCard(name_on_opposite_card);
                expense.setOppositeBank(opposite_bank);
                expense.setOppositeCardNo(opposite_card_no);
                expense.setPostDate(yyyy_MM_dd_PARSER.parse(post_date));
                expense.setTransDate(yyyy_MM_dd_HH_mm_ss_PARSER.parse(post_date+" 00:00:00"));
                expense.setTransChannel(trans_channel);
                expense.setTransMethod(trans_method);
                // int indexOf = billDetailPage.indexOf("币种");
                // String currency_type =
                // CreditResultUtil.convertCurr(billDetailPage.substring(indexOf
                // + 3, indexOf + 6));
                expense.setCurrencyType("CNY");
                shopping_sheets.add(expense);
            }
        }
    }
}
/**
 * 在时尚版网银中抓取信用卡账户信息
 *
 * @param taskkey
 */
function crawlCreditCardsOnNewEdition(taskkey) {
    var list=new ArrayList();
    context.put("credit_data", list);
    //获取账户的基础信息
    var indexUrl = "https://perbank.abchina.com/EbankSite/index.do";
    var indexHtml = ajax_get(indexUrl);
    storeDataToFile(context,indexHtml, "index_fashionEdition.html");
    var cardInfo = RegexUtils.GetText(indexHtml, " var cardInfo = JSON.parse\\(\\'*(.*)").replaceAll(";", "").replaceAll("\'", "").replaceAll("\\)", "");
    var listJsonObject = null;
    try {
        listJsonObject = JSON.toJavaObject(JSON.parseArray(cardInfo), List.class);
    } catch (e) {
        logger.info(e);
    }
    if (listJsonObject == null || listJsonObject.size() == 0) {
        logger.info("ABCBank:taskKey=" + taskkey + "获取账户信息失败，停止抓取");
        return;
    }
    var card_num = null;
    var cardNumList=new ArrayList();
    //card_type为2的时候为信用卡
    for(var i=0;i<listJsonObject.size();i++){
        var jsonObject = listJsonObject.get(i);
        var card_type = jsonObject.getString("card_type");
        if (card_type.equals("2")) {
            card_num = jsonObject.getString("card_no");
            if (card_num.length() > 0) {
                cardNumList.add(card_num);
                logger.info("当前账户有信用卡账户信息,卡号为{}",card_num);
            } else {
                logger.info("信用卡号为空");
            }
        }
    }
    logger.info("ABCBank taskkey={}获取到信用卡{}张",taskkey,cardNumList.size());
    if (cardNumList.size()==0) {
        logger.info("ABCBank taskkey={}当前账户没有获取到信用卡卡号相关信息，停止抓取");
        return;
    }
    //循环抓取卡列表
    for(var i=0;i<cardNumList.size();i++){
        var cardnum = cardNumList.get(i);
        creditCardNum=(i+1);
        logger.info("抓取卡号为{}的账户信息",cardnum);
        var creditCardAllData = new CreditCardAllData();
        list.add(creditCardAllData);
        creditCardAllData.setBankName("农业银行");
        creditCardAllData.setCardId(UUID.randomUUID().toString());
        creditCardAllData.setCardType("信用卡");
        creditCardAllData.setFullCardNum(cardnum);
        creditCardAllData.setCardNum(cardnum.substring(cardnum.length() - 4, cardnum.length()));
        var name_on_card = RegexUtils.GetText(indexHtml, " var custName = (.*)").replaceAll("\"", "").replaceAll(";", "").replaceAll("\\\\", "");
        creditCardAllData.setNameOnCard(name_on_card);
        var param = new LinkedHashMap();
        // ccCard=6259980002580486
        param.put("ccCard", cardnum);
        var creditCardInfo = ajax_post("https://perbank.abchina.com/EbankSite/CreditCardDetailAct.do", param);
        storeDataToFile(context,creditCardInfo, creditCardNum+"_creditCardInfo.html");
        try {
            var object =  JSON.parse(creditCardInfo);
            var jsonObject = object.getJSONArray("ccCardPaymentInfo").getJSONObject(0);
            var credit_limit = jsonObject.getString("dAMT_CRLM");
            var cash_limit = jsonObject.getString("dAMT_CRLM_CSH");
            billDate = jsonObject.getString("szBILL_DAY");
            var balance = jsonObject.getString("dAMT_CUMP");
            var cash_balance = jsonObject.getString("dAMT_CSH");
            creditCardAllData.setCreditLimit(CreditResultUtil.StringToBigDecimal(credit_limit));
            creditCardAllData.setCashLimit(CreditResultUtil.StringToBigDecimal(cash_limit));
            creditCardAllData.setBalance(CreditResultUtil.StringToBigDecimal(balance));
            creditCardAllData.setCashBalance(CreditResultUtil.StringToBigDecimal(cash_balance));
        } catch ( e) {
            logger.info("ABC taskKey={} 获取账户额度等信息失败",context.get(LUA_TASK_KEY));
        }
        logger.info("ABC taskKey ={} ,抓取未出账单信息",context.get(LUA_TASK_KEY));
        crawlUndoneOnNewEdition(creditCardAllData, luaFuncs);
        logger.info("ABC taskKey= {} ,抓取已出账单信息",context.get(LUA_TASK_KEY));
        crawlDoneOnNewEdition(creditCardAllData);
        //取最后一个设置最后一个last_balance为null
        var bills = creditCardAllData.getBills();
        if (bills!==null && bills.size()>0) {
            var billAllData = bills.get(bills.size()-1);
            billAllData.setLastBalance(null);
        }
    }
}


/**
 * 时尚版抓取信用卡未出账单
 *
 * @param creditCardAllData
 * @param client
 */
function crawlUndoneOnNewEdition( creditCardAllData) {
    var billAllData = new BillAllData();
    var param=new LinkedHashMap();
    //ccCardBillQry=6259980002580486&billDate=20170907
    var spd = new SimpleDateFormat("YYYY-MM");
    var cld = Calendar.getInstance();
    var day = cld.get(Calendar.DAY_OF_MONTH);
    var billday = 0;
    try {
        billday = Integer.parseInt(billDate);// 这个数字在抓取基本信息的时候已经设置号为纯数字 21 7

    } catch (e) {
        logger.info("账单日解析识别，继续抓取,billdate的值为" + billDate);
    }
    //账单日 7号 今天大于七号，就是
    if (day <= billday) {
        //cld.add(Calendar.MONTH, -1);
    }
    var parambilldate = spd.format(cld.getTime());
    param.put("ccCardBillQry", creditCardAllData.getFullCardNum());
    param.put("billDate",parambilldate.replaceAll("-", "")+billDate );
    var url = "https://perbank.abchina.com/EbankSite/ccBillwczdAct.do";
    var undoneBillInfo = ajax_post(url, param);//现在参数不对
    storeDataToFile(context,undoneBillInfo, creditCardNum + "_credit__bill_shopping_sheets_" + index + ".html");
    index = index + 1;
    var undoneBillJson = RegexUtils.GetText(undoneBillInfo, "var lQryList =*(.*)").replaceAll(";", "");
    billAllData.setBankName("农业银行");
    creditCardAllData.getBills().add(billAllData);
    billAllData.setBillId(UUID.randomUUID().toString());
    billAllData.setBillType("UNDONE");
    if (day > billday) {
        cld.add(Calendar.MONTH, 1);
    }
    var billdate = spd.format(cld.getTime());
    billAllData.setBillMonth(billdate);

    //获取账单日失败的时候 赋予账单日默认值 07
    if(null == billDate || equals(billDate,"")){
        this.billDate = "07";
    }

    billAllData.setBillDate(yyyy_MM_dd_PARSER.parse(billdate + "-" + this.billDate));
    billAllData.setCashLimit(creditCardAllData.getCashLimit());
    billAllData.setCreditLimit(creditCardAllData.getCreditLimit());
    billAllData.setLastPayment(null);
    billAllData.setUsdCashLimit(null);
    billAllData.setInterest(null);
    billAllData.setUsdInterest(null);
    billAllData.setUsdCreditLimit(null);
    billAllData.setUsdLastBalance(null);
    billAllData.setUsdLastPayment(null);
    billAllData.setUsdMinPayment(null);
    billAllData.setUsdNewBalance(null);
    billAllData.setUsdNewCharges(null);
    billAllData.setMinPayment(null);
    if (undoneBillJson.equals("null") || undoneBillJson == null || undoneBillJson.length == 0) {
        logger.info("当前信用卡无未出账单，停止抓取未出账单信息");
        billAllData.setNewBalance(null);
        billAllData.setNewCharges(null);
        billAllData.setMinPayment(null);
        return;
    }
    var list = null;
    try {
        list = JSON.toJavaObject(JSON.parseArray(undoneBillJson), List.class);
    } catch (e) {
        logger.info("未出账单转换为jsonobject集合失败,结束抓取");
        e.printStackTrace();
        return;
    }
    logger.info("解析未出账单的信息");
    try {
        parseUndoneBillDetailOnNewEdition(billAllData, list);
    } catch (e) {
        e.printStackTrace();
        logger.info("未出账单解析失败,结束抓取，可能含有美元账单信息，错误信息为{}",e);
    }

}



/**
 * 解析未出账单的信息  时尚版
 *
 * @param billAllData
 */
function parseUndoneBillDetailOnNewEdition( billAllData, list) {
    if (list != null && list.size() > 0) {
        var shopping_sheets = billAllData.getShoppingSheets();
        var order_index = shopping_sheets.size() + 1;
        for(var i=0;i<list.size();i++){
            var jsonObject = list.get(i);
            var expense = new Expense();
            var transDate = jsonObject.getString("szDATE_TR");//交易日期
            if (transDate==null) {
                transDate="";
            }
            var post_date = jsonObject.getString("szDATE_POST");//记账日期
            if (post_date==null) {
                transDate="";
            }
            var card_num = jsonObject.getString("szNUM_CARD");// 全卡号
            if (card_num==null) {
                card_num="";
            }
            var descrtipion = jsonObject.getString("szTXT_TR");//描述
            if (descrtipion==null) {
                descrtipion="";
            }
            var trans_addr = jsonObject.getString("szTXT_ADDR_TR");//交易地点
            if (trans_addr==null) {
                trans_addr="";
            }
            var amount_money = jsonObject.getString("dAMT_POST");//交易金额
            if (amount_money==null) {
                amount_money="";
            }
            var shouruFlag = jsonObject.getString("szIND_MSK_DTAL");//判断是否为收入或者支出 以此判断政府
            if (shouruFlag==null) {
                shouruFlag="";
            }
            var currecy=jsonObject.getString("szCOD_CCY_POST");//币种
            if (currecy==null) {
                currecy="";
            }
            // jsonObject.getString("");
            expense.setId(UUID.randomUUID().toString());
            expense.setTransDate(yyyy_MM_dd_HH_mm_ss_PARSER.parse(formatDate(transDate)+ " 00:00:00"));
            if (card_num!=null&&card_num.length > 4) {
                expense.setCardNum(card_num.substring(card_num.length - 4, card_num.length));
            }
            expense.setTransAddr(trans_addr);
            expense.setPostDate(yyyy_MM_dd_PARSER.parse(formatDate(post_date) ));
            //使用新的消费类型判断
            if (amount_money.contains("-")) {
                amount_money=amount_money.replaceAll("-", "");
            }else{
                amount_money="-"+amount_money;
            }
            var category=CategoryUtil.getCreditCategory("ABC", descrtipion, amount_money);
            amount_money=amount_money.replaceAll("-", "");
            expense.setCategory(category);
            expense.setOrderIndex(order_index);
            expense.setBalance(null);
            order_index++;
            expense.setDescription(descrtipion);
            if (currecy.contains("01")) {
                //amount_money = amount_money.substring(0, amount_money.length() - 3);
                expense.setCurrencyType("CNY");
            }else if (currecy.contains("14")) {
                logger.info("当前账户具有美元信息，继续抓取");
                //amount_money = amount_money.substring(0, amount_money.length() - 2);
                //这里故意写错，如果有美元账单的时候会报错
                expense.setCurrencyType("USD");
                var usd_new_balance = billAllData.getUsdNewBalance();
                if (usd_new_balance!=null) {
                    usd_new_balance.add(CreditResultUtil.StringToBigDecimal(amount_money));
                    billAllData.setUsdNewBalance(usd_new_balance);
                }else{
                    usd_new_balance=new BigDecimal("0");
                    usd_new_balance.add(CreditResultUtil.StringToBigDecimal(amount_money));
                    billAllData.setUsdNewBalance(usd_new_balance);
                }
                if (!descrtipion.contains("转入")){
                    var usd_new_charges = billAllData.getUsdNewCharges();
                    if(usd_new_charges != null || typeof(usd_new_charges) != "undefined" ){
                       usd_new_charges.add(CreditResultUtil.StringToBigDecimal(amount_money));
                       billAllData.setUsdNewCharges(usd_new_charges);
                   }else {
                       usd_new_charges=new BigDecimal("0");
                       usd_new_charges.add(CreditResultUtil.StringToBigDecimal(amount_money));
                       billAllData.setUsdNewCharges(usd_new_charges);
                   }
                }
            }
            expense.setAmountMoney(CreditResultUtil.StringToBigDecimal(amount_money));
            if (shouruFlag.length>4) {
                shouruFlag=shouruFlag.substring(shouruFlag.length-1,shouruFlag.length);
            }
            if (descrtipion.contains("转入") || descrtipion.contains("还款") || shouruFlag.equals("2")) {
                expense.setAmountMoney(expense.getAmountMoney().multiply(new BigDecimal("-1")));
            }else{
                //当新增的款额不为负的时候，全部想加为new_charges
                var new_charges = billAllData.getNewCharges();
                new_charges=new_charges.add(expense.getAmountMoney());
                billAllData.setNewCharges(new_charges);
            }
            //增加未出账单的new_balance
            var new_balance = billAllData.getNewBalance();
            new_balance=new_balance.add(expense.getAmountMoney());
            billAllData.setNewBalance(new_balance);

            shopping_sheets.add(expense);
        }
    } else {
        logger.info("当前账户无账单明细");
    }
}
/**
 * 抓取时尚版已出账单
 *
 * @param creditCardAllData
 * @param client
 */
function crawlDoneOnNewEdition( creditCardAllData) {
    //获取可以查询账单的月份 正则取出月份 循环抓取
    var url = "https://perbank.abchina.com/EbankSite/ccBillQryAct.do";
    var param = new LinkedHashMap();
    param.put("ccCardBillQry", creditCardAllData.getFullCardNum());
    var initHtml = ajax_post(url, param);
    storeDataToFile(context,initHtml, creditCardNum+"_queryMonth.html");
    var queryAllMonth = RegexUtils.GetText(initHtml, "var convertListStr =*(.*)").replaceAll(";", "");
    var list = null;
    try {
        list = JSON.toJavaObject(JSON.parseArray(queryAllMonth), List.class);
    } catch ( e) {
        e.printStackTrace();
        logger.info("所有能查询的月份解析失败，停止抓取未出账单");
    }
    if (list == null || list.size() == 0) {
        logger.info("无可以查询的月份，停止抓取");
        return;
    }
    var num = 1;
    for(var i=0;i<list.size();i++){
        var jsonObject = list.get(i);
        var billDate = jsonObject.getString("billDate");
        if (billDate != null && billDate.length() > 0) {
            logger.info("抓取信用卡{}月账单", billDate);
            try {
                crawlDoneDetailOnNewEdition(creditCardAllData, param, billDate, num);
            } catch (e) {
                logger.info("抓取{}月账单失败，停止抓取，继续抓取下一个月");
                logger.info("具体的错误信息为:{}",printStackTraceToString(e));
            }
            num++;
        }
    }

}


/**
 * 时尚版 抓取已出账单
 *
 * @param creditCardAllData
 * @param client
 * @param param
 * @param billdate 账单日
 * @param num 当前第几张卡
 * @throws InterruptedException
 */
function crawlDoneDetailOnNewEdition( creditCardAllData, param, billdate,  num) {
    var billAllData = new BillAllData();
    param.put("billDate_reqry", billdate);
    param.put("ccBillQryForm_bk", "");
    Thread.sleep(1000);
    //获取账单信息，包括new_balance
    var billlDateHtml = ajax_post("https://perbank.abchina.com/EbankSite/ccBillQry2Act.do", param);
    storeDataToFile(context,billlDateHtml, creditCardNum + "_credit__bills_" + num + ".html");
    //通过正则获取的信息为json格式，通过转为jsonArray对象来获取数据
    var billInfoStr = RegexUtils.GetText(billlDateHtml, "var billInfoList = *(.*)").replaceAll(";", "");
    var array = JSON.parse(billInfoStr);
    billAllData.setBankName("农业银行");
    billAllData.setBillDate(yyyy_MM_dd_PARSER.parse(formatDate(billdate)));
    billAllData.setBillId(UUID.randomUUID().toString());
    billAllData.setBillType("DONE");
    billAllData.setBillMonth(formatMonth(billdate));//amtPmtdAll
    billAllData.setCashLimit(creditCardAllData.getCashLimit());

    billAllData.setUsdCashLimit(null);
    billAllData.setInterest(null);
    billAllData.setUsdInterest(null);
    billAllData.setUsdCreditLimit(null);
    billAllData.setUsdLastBalance(null);
    billAllData.setUsdLastPayment(null);
    billAllData.setUsdMinPayment(null);
    billAllData.setUsdNewBalance(null);
    billAllData.setUsdNewCharges(null);
    billAllData.setLastPayment(null);
    creditCardAllData.getBills().add(billAllData);
    //此情况为json数组长度为0，代表无数据
    if (array.length == 0 || array == null) {
        logger.info("当前月{}无账单信息,停止抓取该月，继续抓取下一个月", billdate);
        billAllData.setCreditLimit(creditCardAllData.getCreditLimit());
        billAllData.setNewCharges(null);
        billAllData.setNewBalance(null);
        billAllData.setMinPayment(null);
        if (creditCardAllData.getBills().size() >= (num - 1) && num - 1 >= 0) {
            var lastBill = creditCardAllData.getBills().get(num - 1);
            lastBill.setLastBalance(null);
        }
        return;
    } else {
        logger.info("当前月{}有账单信息，继续抓取该月", billdate);

    }
    var billInfoJSon = array.getJSONObject(0);
    //取数据
    var pay_ment_duedate = RegexUtils.GetText(billlDateHtml, "var dueDate =*(.*)").replaceAll(";", "").replaceAll("\"", "");
    var credit_limit = billInfoJSon.getString("amtCrlm");
    var new_charges = billInfoJSon.getString("amtDbNew");
    var min_payment = billInfoJSon.getString("amtDue");
    var last_payment = billInfoJSon.getString("amtCrNew");
    var new_balance = billInfoJSon.getString("amtPmtdAll");
    var currency = billInfoJSon.getString("codCcyEn");//
    billAllData.setPaymentDueDate(yyyy_MM_dd_PARSER.parse(formatDate(pay_ment_duedate)));
    if (currency.equals("USD")) {
        logger.info("ABC taskKey={}当前账单含有美元信息，", context.get(LUA_TASK_KEY));
        billAllData.setUsdLastBalance(new BigDecimal("0"));
        billAllData.setUsdLastPayment(CreditResultUtil.StringToBigDecimal("-" + last_payment));
        billAllData.setUsdMinPayment(new BigDecimal("0"));
        billAllData.setUsdNewBalance(CreditResultUtil.StringToBigDecimal(new_balance));
        billAllData.setUsdNewCharges(CreditResultUtil.StringToBigDecimal(new_charges));
    }
    billAllData.setCreditLimit(CreditResultUtil.StringToBigDecimal(credit_limit));
    billAllData.setCashLimit(creditCardAllData.getCashLimit());
    //billAllData.setCashLimit(billAllData.getCreditLimit().divide(new BigDecimal("2")));
    billAllData.setNewBalance(CreditResultUtil.StringToBigDecimal(new_balance));
    if (creditCardAllData.getBills().size() > 0 && num == 1) {//即有未出账单  且为第一次循环，取第一次账单的newbalance
        var billAllData2 = creditCardAllData.getBills().get(0);
        if (billAllData2.getBillType().equals("UNDONE")) {
            //当前new_balance可能为null 当上一期未出账单为空的时候可能会出现空指针
            var undoneNewBalance = billAllData2.getNewBalance();
            if (undoneNewBalance == null) {
                undoneNewBalance = new BigDecimal("0");
            }
            billAllData2.setNewBalance(undoneNewBalance.add(CreditResultUtil.StringToBigDecimal(new_balance)));
        }
    }
    billAllData.setLastPayment(CreditResultUtil.StringToBigDecimal("-" + last_payment));
    billAllData.setNewCharges(CreditResultUtil.StringToBigDecimal(new_charges));
    billAllData.setMinPayment(CreditResultUtil.StringToBigDecimal(min_payment));
    billAllData.setCashLimit(creditCardAllData.getCashLimit());
    if (creditCardAllData.getBills().size() >= (num - 1) && num - 1 >= 0) {
        var lastBill = creditCardAllData.getBills().get(num - 1);
        var stringToBigDecimal = CreditResultUtil.StringToBigDecimal(new_balance);
        lastBill.setLastBalance(stringToBigDecimal);
    }
    var card_num = param.get("ccCardBillQry");
    logger.info("账户请求{}月具体的消费明细", billdate);//返回为json数据格式
    //ccCardBillQry=6259980002580486&billDate=20170707
    var map = new LinkedHashMap();
    map.put("ccCardBillQry", card_num);
    map.put("billDate", billdate);
    var billDetailJson = ajax_post("https://perbank.abchina.com/EbankSite/ccBillQry1107Act.do", map);
    storeDataToFile(context,billDetailJson, creditCardNum + "_credit_shopping_sheets" + index + ".html");
    index++;
    try {
        var list = JSON.toJavaObject(JSON.parseArray(billDetailJson), List.class);
        parseDoneBillDetailOnNewEdition(billAllData, list, card_num);
    } catch (e) {
        logger.info("解析{}月具体的消费明细失败");
        e.printStackTrace();
    }
    logger.info("请求{}月分期信息", billdate);//返回为json数据格式
    var installmentJson = ajax_post("https://perbank.abchina.com/EbankSite/ccBillQry2037Act.do", map);
    storeDataToFile(context,installmentJson, creditCardNum + "_credit_installment" + installmentIndex + ".html");
    installmentIndex++;
    try {
        var list = JSON.toJavaObject(JSON.parseArray(installmentJson), List.class);
        //todo 解析分期账单数据
        parseInstallmentOnNewEdition(billAllData, list, billdate);
    } catch (e) {
        logger.info("解析{}月具体的分期失败,具体原因为{}", billdate, e);
        e.printStackTrace();
    }





}
/**
 * 解析分期数据
 * @param billAllData
 * @param list
 * @param billDate
 */
function parseInstallmentOnNewEdition( billAllData, list, billDate){
    try {
        if (list!==null &&list.size()>0) {
            var installments = new ArrayList();
            var handFeeList=new LinkedList();//分期手续费的集合
            var shoppingSheests = billAllData.getShoppingSheets();
            for(var i = 0;i<shoppingSheests.size();i++){
                var description = shoppingSheests.get(i).getDescription();
                if(description.contains("手续费")){
                    var installmentVo=new Installment();
                    installmentVo.setTransferfeeDesc("");
                    var handFee=shoppingSheests.get(i).getAmountMoney();
                    installmentVo.setHandingFee(CreditResultUtil.StringToBigDecimal(handFee));
                    installmentVo.setHandingfeeDesc(description);
                    handFeeList.add(installmentVo);
                }
            }
            for(var i=0;i<list.size();i++){
                //[{"billCnt":"3","remainCnt":"3","insmType":"账单分期","codCcy":"01","openDate":"20180522","capAmt":"5000.00","feeAmt":"833.00"}]
                var jsonObject = JSON.parseObject(list.get(i).toString());
                var installment=new Installment();
                var installment_type = jsonObject.getString("insmType");
                var start_date = jsonObject.getString("openDate");//分期日期
                var allmoney = jsonObject.getString("capAmt");//总金额/币种
                // var totalCount = jsonObject.getString("cntOccrTot");//总期数
                var currCount = jsonObject.getString("billCnt");//当前
                var remainCount = jsonObject.getString("remainCnt");//剩余期数
                var shouldpay = jsonObject.getString("feeAmt");//本期应还
                //String remainpay = jsonObject.getString("amtCapDue");//声乐本金
                var currecy=jsonObject.getString("codCcy");
                installment.setShoppingsheetId(UUID.randomUUID().toString());
                installment.setAmountMoney(CreditResultUtil.StringToBigDecimal(shouldpay));
                installment.setInstallmentType(judgeInstallmentType(installment_type));
                installment.setInstallmentDesc(installment_type);
                installment.setTransDate(yyyy_MM_dd_HH_mm_ss_PARSER.parse(formatDate(start_date)+" 00:00:00"));
                installment.setTransferFee(null);
                installment.setBillId(null);
                installment.setPostDate(null);
                var currentMonth = Integer.parseInt(currCount);
                installment.setCurrentMonth(currentMonth);
                var reaminMonth=Integer.parseInt(remainCount);
                installment.setTotalMonth(reaminMonth+currentMonth);
                if (equals("01",currecy)) {
                    installment.setCurrencyType("CNY");
                }else if (currecy.equals("14")) {
                    installment.setCurrencyType("USD");
                }else {
                    installment.setCurrencyType("CNY");
                }
                installments.add(installment);
            }
            //todo 这个地方有问题
            parseInstallmentAndHandFee(billAllData, installments, handFeeList);
            billAllData.setInstallments(installments);
            logger.info("分析当前月{}账单与分期，分别对应分期和分s期手续费");
        }else{
            logger.info("当前月{}无分期数据，停止解析",billDate);
        }
    }catch (e) {
        logger.info("当前月{}无分期数据，解析出错，错误信息：{}",billDate,e);
    }
}

/**
 *  将分期本金和手续费分别对应起来
 * @param billAllData
 * @param installmentList 分期本金集合
 * @param handFeeList 分期手续费集合
 */
function parseInstallmentAndHandFee( billAllData,installmentList, handFeeList){
    logger.info("分别对应{}月账单的分期分期手续费",billAllData.getBillMonth());
    bubbleSort(installmentList,"");
    bubbleSort(handFeeList, "handfee");
    if (handFeeList.size()==installmentList.size()) {
        for (var i = 0; i < installmentList.size(); i++) {
            var installment = installmentList.get(i);
            var handFee = handFeeList.get(i);
            installment.setHandingFee(handFee.getHandingFee());//
            installment.setHandingfeeDesc(handFee.getHandingfeeDesc());
        }
    }else{
        logger.info("ABCBank taskkey={}分期手续费与分期对应出错,即分期和本金并不对应，分期的数量为{}，手续费的数量为{}",context.get(LUA_TASK_KEY),installmentList.size(),handFeeList.size());
        for(var i=0;i<installmentList.size();i++){//当现在的本金和 手续费都是按照费用从大到小的顺序排序
            var installment = installmentList.get(i);
            if ((i+1)<=handFeeList.size()) {
                var handFee = handFeeList.get(i);
                installment.setHandingFee(handFee.getHandingFee());
                installment.setHandingfeeDesc(handFee.getHandingfeeDesc());
            }else{
                installment.setHandingFee(null);//如果当前分期对应的手续没有，那么手续费设置为null
                installment.setHandingfeeDesc("");
            }

        }
    }
    // billAllData.setInstallments(installmentList);
}
/**
 * 根据分期的金额来排序 从小到大来排序
 * @param list
 */
function bubbleSort( list,flag){
    for(var i=0;i<list.size()-1;i++){
        for(var j=0;j<list.size()-1;j++){
            var currentBigdecimal=null;
            var nextBigdecimal=null;
            if ("".equals(flag)) {//为空的时候取本金
                currentBigdecimal=list.get(j).getAmountMoney();
                nextBigdecimal=list.get(j+1).getAmountMoney();
            }else{//不为空的时候取手续费
                currentBigdecimal=list.get(j).getHandingFee();
                nextBigdecimal=list.get(j+1).getHandingFee();//
            }
            if (nextBigdecimal.compareTo(currentBigdecimal)==1) {//当前分期的金额大于下一个
                //int temp=list.get(j);
                var tempInstallment=list.get(j);
                list.set(j, list.get(j+1));
                list.set(j+1, tempInstallment);
            }
        }
    }
}
/**
 * 根据分期的描述来判断是否属于哪种分期类型
 * @param installmentDescrption
 * @return
 */
function judgeInstallmentType(installmentDescrption){
    var installmentType="CONSUME";
    if (installmentDescrption!=null && installmentDescrption.length>0) {
        if (installmentDescrption.contains("交易分期")) {
            installmentType="CONSUME";
        }else if (installmentDescrption.contains("现金")) {
            installmentType="CASH";
        }else if (installmentDescrption.contains("账单")) {
            installmentType="BILL";
        }
    }
    //todo
    return installmentType;
}

/**
 * 解析已出账单的明细
 * @param billAllData
 * @param list
 */
function parseDoneBillDetailOnNewEdition( billAllData, list, cardNum){
    if (list!=null &&list.size()>0) {
        var shopping_sheets = billAllData.getShoppingSheets();
        var order_index=shopping_sheets.size()+1;
        for(var i=0;i<list.size();i++){
            var jsonObject = list.get(i);
            var expense=new Expense();
            var tranDate=jsonObject.getString("trDate");//交易
            var recordDate = jsonObject.getString("postDate");//记账日期
            var transRemark=jsonObject.getString("trTxt");//描述
            var addr=jsonObject.getString("trAddress");//垫底
            var transAmount=jsonObject.getString("postAmt");//消费金额
            var currecyType = jsonObject.getString("postCod");//币种  01代表人民币
            expense.setId(UUID.randomUUID().toString());
            cardNum=cardNum.substring(cardNum.length()-4,cardNum.length());
            expense.setCardNum(cardNum);
            if (StringUtils.isNotBlank(recordDate)) {
                // 正则表达式，将20171010格式转化为2017-10-10
                recordDate = recordDate.replaceAll("(?<=^\\d{4})", "-").replaceAll("(?<=^\\d{4}\\-\\d{2})", "-");
            }
            expense.setTransAddr(addr);
            expense.setOrderIndex(order_index);
            order_index++;
            expense.setPostDate(yyyy_MM_dd_PARSER.parse(recordDate));
            // 格式化日期
            if (StringUtils.isNotBlank(tranDate)) {
                tranDate = tranDate.replaceAll("(?<=^\\d{4})", "-").replaceAll("(?<=^\\d{4}\\-\\d{2})", "-");
            }
            expense.setTransDate(yyyy_MM_dd_HH_mm_ss_PARSER.parse(tranDate+ " 00:00:00"));// 格式化日期
            if (StringUtils.isNotBlank(transAmount)) {
                // 消费金额应该为正
                expense.setAmountMoney(
                    CreditResultUtil.StringToBigDecimal(transAmount).multiply(new BigDecimal("-1")));
            } else {
                expense.setAmountMoney(new BigDecimal("0"));
            }
            if (currecyType.equals("01")) {
                expense.setCurrencyType("CNY");
            }else if (currecyType.equals("14")) {
                expense.setCurrencyType("USD");
                logger.info("有美元账单信息，需要继续抓取美元");
            }else {
                expense.setCurrencyType("CNY");
            }
            expense.setBalance(null);
            expense.setDescription(transRemark);
            //使用新的消费类型判断
            if (transAmount.contains("-")) {
                transAmount=transAmount.replaceAll("-", "");
            }else{
                transAmount="-"+transAmount;
            }
            var category=CategoryUtil.getCreditCategory("ABC", transRemark, transAmount);
            expense.setCategory(category);// 判断消费类型
            shopping_sheets.add(expense);
        }
    }
}
function printStackTraceToString(t) {
    var sw = new StringWriter();
    t.printStackTrace(new PrintWriter(sw, true));
    return sw.getBuffer().toString();
}

/**
 * 根据密码长度来获取*的长度
 * @param password
 * @return
 */
function getAnonymousString(password) {
    var anonymousPasswd="";
    if (password!==null && password.length>0) {
        for (var i = 0; i < password.length; i++) {
            anonymousPasswd=anonymousPasswd+"*";
        }
    }
    return anonymousPasswd;
}
/**
 * 复制文件到指定目录
 * @param context
 * @param oldFile
 * @param fileName
 */
function filePathToLocal(context,oldFile,fileName,taskKey) {
    try {
        var year = taskKey.substring(0, 2);
        var month = taskKey.substring(2, 4);
        var day = taskKey.substring(4, 6);
        var filePath = PropertyManager.getRB("htmlStorage", "home_path") +"\\" +"BANK" + "\\" +
            year + "\\" + month + "\\"  + day + "\\" + taskKey + "\\" + fileName;
        logger.error("浦发借记卡，filePath:{}", filePath);
        FileUtils.copyFile(new File(oldFile),new File(filePath));
    }catch (e) {
        statCaughtException(context, e);
        logger.error("浦发借记卡，在filePathToLocal 方法中保存交易流水信息时出错，taskKey:{},{}", taskKey, e);
    }
}

var luaFuncs = {
    getCookie: function () {
        var map = new HashMap();
        map.put("method", "getCookie");
        return sendAndReceive(map)
    },

    /**
     * 获取进程锁
     * @param lockName
     * @return
     */
    getABCBankLock:function (lockName) {
        var map=new LinkedHashMap();
        map.put("method", "getABCBankLock");
        map.put("lockName", lockName);
        return sendAndReceive(map);
    },
    /**
     * 释放进程锁
     * @param lockName
     * @return
     */
    releaseABCBankLock :function(lockName) {
        var map=new LinkedHashMap ();
        map.put("method", "releaseABCBankLock");
        map.put("lockName", lockName);
        return sendAndReceive(map);
    },

    /**
     * 在ie窗口在hidden的状态下，根据id获取图片
     * @return
     */
    getImgByIdOnHiden:function (image_id){
        var map=new LinkedHashMap ();
        map.put("method", "getImgByIdOnHiden");
        map.put("image_id", image_id);
        return sendAndReceive(map);
    },

    /**
     * 去除掉 在登录后首页的悬浮框体
     * @return
     */
    wipeSuspensionframe:function () {
        var map=new LinkedHashMap();
        map.put("method", "wipeSuspensionframe");
        return sendAndReceive(map);
    },


    /**
     * 使用的根据id点击然后等待页面跳转
     * @param click_id
     * @return
     */
    submitSMS: function(click_id) {
        var map=new LinkedHashMap();
        map.put("method", "submitSMS");
        map.put("click_id", click_id);
        return sendAndReceive(map);
    },

    /**
     * 设置窗口大小
     * @return
     */
    setSize:function(){
        var map=new LinkedHashMap();
        map.put("method", "setSize");
        return sendAndReceive(map);
    },

    /**
     *
     * @return
     */
    showNoTop:function(){
        var map=new LinkedHashMap();
        map.put("method", "showNotTop");
        return sendAndReceive(map);
    },



    /**
     * 登录用户名和密码输入
     * @param username
     * @param password
     * @return
     * @throws Exception
     */
    loginUsernameAndPassowrd:function(username,password, taskKey)  {
        var map=new LinkedHashMap();
        map.put("method", "loginUsernameAndPassword");
        map.put("username", username);
        map.put("password", password);
        map.put("taskKey", taskKey);
        lp.Send(JSON.toJSONString(map));
        var anonymousPassword = getAnonymousString(password);
        var anonymousUsername=getAnonymousString(username);
        map.put("password", anonymousPassword);
        map.put("username", anonymousUsername);
        logger.info("send msg : {}", JSON.toJSONString(map));
        return lp.Receive();

    },



    /**
     * 以GB2312方法获取页面
     * @return
     */
    getHtmlGb2312:function () {
        var map=new LinkedHashMap();
        map.put("method", "getHtmlGb2312");
        return sendAndReceive(map);
    },

    /**
     * 截取当前屏幕的状态
     * @return
     */
    getScreenShots :function() {
        var map=new LinkedHashMap();
        map.put("method", "getScreenShots");
        return sendAndReceive(map);
    },


    setSizeAndShow:function(){
        var map=new LinkedHashMap();
        map.put("method", "setSizeAndShow");
        return sendAndReceive(map);
    },

    /**
     * 登录-设置验证码的方法
     * @return 是否登录成功的标志
     * @throws Exception
     */
    loginCheckCode:function(code)  {
        var map=new LinkedHashMap();
        map.put("method", "loginCheckCode");
        map.put("code", code);
        return sendAndReceive(map);
    },
    /**
     * ajax获取文件下载的方法
     * @return 是否登录成功的标志
     * @throws Exception
     */
    ajaxGetFile:function (url,datas) {
        var  map=new LinkedHashMap();
        map.put("method", "ajaxGetFile");
        map.put("url",url);
        map.put("data", datas);
        return sendAndReceive(map);
    },

    abcSetInputEvent: function (code, inputId) {
        var map = new LinkedHashMap();
        map.put("method", "abcSetInputEvent");
        map.put("code",code);
        map.put("inputId", inputId);
        return sendAndReceive(map);
    },

    /**
     * 登录成功后 需要短信验证码
     * @param msg
     * @return
     */
    checkMsg:function(msg){
        var map=new LinkedHashMap();
        map.put("method", "checkMsg");
        map.put("msg", msg);
        return sendAndReceive(map);
    },

    /**
     * 根据taskkey复制文件path到与当前文件同目录下的文件夹下
     * @param path 需要复制文件的路径
     * @param taskKey
     * @param name
     * @param copyPath 需要复制到的文件夹的路径
     * @return
     */
    copyFile:function ( path, taskKey,  name, copyPath) {
        var map=new LinkedHashMap();
        map.put("method", "copyFile");
        map.put("path", path);
        map.put("name", name);
        map.put("copyPath", copyPath);
        map.put("taskkey", taskKey);
        return sendAndReceive(map);
    },
    /**
     * 根据url地址进行跳转
     * @return
     */
    jumpByUrl:function (url) {
        var map=new LinkedHashMap();
        map.put("url", url);
        map.put("method", "jumpByUrl");
        return sendAndReceive(map);
    },


    /**
     * 跳转到新版页面的js方法
     * @return
     */
    jumpNewEdition:function () {
        var map=new LinkedHashMap();
        map.put("method", "jumpNewEdition");
        return sendAndReceive(map)
    },
    /**
     * 刷新当前页面的方法
     * @return
     */
    abcRefresh:  function (){
        var map=new LinkedHashMap();
        map.put("method", "abcRefresh");
        return sendAndReceive(map);
    },
    checkControlAvailable:function(){
        var map = new HashMap();
        map.put("method","checkControlAvailable");
        return sendAndReceive(map)
    },
    /**
     * 点击登录按钮
     * @returns {*}
     */
    click_login_button: function () {
        var map = new LinkedHashMap();
        map.put("method", "click_login_button");
        return sendAndReceive(map);
    },
    /**
     * 输入失去焦点事件
     * @param id
     * @returns {*}
     */
    input_text_by_id_event: function (id,value) {
        var map = new LinkedHashMap();
        map.put("method", "input_text_by_id_event");
        map.put("id",id);
        map.put("text",value);
        return sendAndReceive(map);
    },
    /**
     * 等待跳转的url
     * @returns {*}
     */
    waiting_Goto: function (hostMills) {
        var map = new LinkedHashMap();
        map.put("method", "waiting_Goto");
        map.put("hostMills",hostMills)
        return sendAndReceive(map);
    },
    /**
     * 增加等待页面加载时间
     * @param time
     * @returns {*}
     */
    sleep_Wait: function () {
        var map = new LinkedHashMap();
        map.put("method", "sleep_Wait");
        return sendAndReceive(map);
    },
    /**
     * 增加等待页面加载时间
     * @param time
     * @returns {*}
     */
    click_fashion_button: function () {
        var map = new LinkedHashMap();
        map.put("method", "click_fashion_button");
        return sendAndReceive(map);
    }
}